diff --git a/problems/binary-tree-inorder-traversal.rs b/problems/binary-tree-inorder-traversal.rs new file mode 100644 index 0000000..96d685a --- /dev/null +++ b/problems/binary-tree-inorder-traversal.rs @@ -0,0 +1,40 @@ +// Definition for a binary tree node. +// #[derive(Debug, PartialEq, Eq)] +// pub struct TreeNode { +// pub val: i32, +// pub left: Option>>, +// pub right: Option>>, +// } +// +// 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, node: Option<&Rc>>) { + 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>>) -> Vec { + let mut values: Vec = Vec::new(); + + Solution::inorder(&mut values, root.as_ref()); + + values + } +}