problems(rs): add „94. Binary Tree Inorder Traversal“

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2022-09-08 13:59:46 +02:00
parent 4838f221f3
commit 9f4f02ac24
Signed by: mfocko
GPG key ID: 7C47D46246790496

View 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
}
}