20240210 Finished
This commit is contained in:
parent
5e0ea0deaf
commit
9cbfc4e1c3
|
@ -42,4 +42,5 @@ mod p1696_jump_game_vi;
|
|||
mod lcp30_magic_tower;
|
||||
mod p2641_cousins_in_binary_tree_ii;
|
||||
mod p993_cousins_in_binary_tree;
|
||||
mod p236_lowest_common_ancestor_of_a_binary_tree;
|
||||
mod p236_lowest_common_ancestor_of_a_binary_tree;
|
||||
mod p94_binary_tree_inorder_traversal;
|
62
src/problem/p94_binary_tree_inorder_traversal.rs
Normal file
62
src/problem/p94_binary_tree_inorder_traversal.rs
Normal file
|
@ -0,0 +1,62 @@
|
|||
/**
|
||||
* [94] Binary Tree Inorder Traversal
|
||||
*/
|
||||
pub struct Solution {}
|
||||
|
||||
use crate::util::tree::{TreeNode, to_tree};
|
||||
|
||||
// submission codes start here
|
||||
|
||||
// Definition for a binary tree node.
|
||||
// #[derive(Debug, PartialEq, Eq)]
|
||||
// pub struct TreeNode {
|
||||
// pub val: i32,
|
||||
// pub left: Option<Rc<RefCell<TreeNode>>>,
|
||||
// pub right: Option<Rc<RefCell<TreeNode>>>,
|
||||
// }
|
||||
//
|
||||
// impl TreeNode {
|
||||
// #[inline]
|
||||
// pub fn new(val: i32) -> Self {
|
||||
// TreeNode {
|
||||
// val,
|
||||
// left: None,
|
||||
// right: None
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
impl Solution {
|
||||
pub fn inorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
|
||||
let mut result = Vec::new();
|
||||
let mut stack = Vec::new();
|
||||
let mut root = root;
|
||||
|
||||
while root.is_some() || !stack.is_empty() {
|
||||
while let Some(r) = root {
|
||||
stack.push(Rc::clone(&r));
|
||||
root = r.borrow().left.clone();
|
||||
}
|
||||
|
||||
root = stack.pop();
|
||||
if let Some(r) = root {
|
||||
result.push(r.borrow().val);
|
||||
root = r.borrow().right.clone();
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
// submission codes end
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_94() {
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user