94. Binary Tree Inorder Traversal
Given the root
of a binary tree, return the inorder traversal of its nodes' values.
Example 1:

Input: root = [1,null,2,3] Output: [1,3,2]
Example 2:
Input: root = [] Output: []
Example 3:
Input: root = [1] Output: [1]
Example 4:

Input: root = [1,2] Output: [2,1]
Example 5:

Input: root = [1,null,2] Output: [1,2]
Constraints:
- The number of nodes in the tree is in the range
[0, 100]
. -100 <= Node.val <= 100
Follow up:
Recursive solution is trivial, could you do it iteratively?
Rust Solution
struct Solution;
use rustgym_util::*;
impl Solution {
fn inorder_traversal(root: TreeLink) -> Vec<i32> {
let mut cur = root;
let mut stack: Vec<TreeLink> = vec![];
let mut res = vec![];
while cur.is_some() || !stack.is_empty() {
while let Some(node) = cur {
let left = node.borrow_mut().left.take();
stack.push(Some(node));
cur = left;
}
let node = stack.pop().unwrap().unwrap();
res.push(node.borrow().val);
cur = node.borrow_mut().right.take();
}
res
}
}
#[test]
fn test() {
let root = tree!(1, None, tree!(2, tree!(3), None));
let res = vec![1, 3, 2];
assert_eq!(Solution::inorder_traversal(root), res);
}
Having problems with this solution? Click here to submit an issue on github.