LeetCode/problems/rs/binary-tree-inorder-traversal.rs
Matej Focko 333866d1bc
chore: split solutions by language
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-06-02 17:19:02 +02:00

40 lines
1,019 B
Rust

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