problems(rs): add „94. Binary Tree Inorder Traversal“
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
4838f221f3
commit
9f4f02ac24
1 changed files with 40 additions and 0 deletions
40
problems/binary-tree-inorder-traversal.rs
Normal file
40
problems/binary-tree-inorder-traversal.rs
Normal file
|
@ -0,0 +1,40 @@
|
|||
// 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 {
|
||||
fn inorder(values: &mut Vec<i32>, node: Option<&Rc<RefCell<TreeNode>>>) {
|
||||
match node {
|
||||
None => {},
|
||||
Some(n) => {
|
||||
Solution::inorder(values, (*n).borrow().left.as_ref());
|
||||
values.push((*n).borrow().val);
|
||||
Solution::inorder(values, (*n).borrow().right.as_ref());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn inorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
|
||||
let mut values: Vec<i32> = Vec::new();
|
||||
|
||||
Solution::inorder(&mut values, root.as_ref());
|
||||
|
||||
values
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue