199. Binary Tree Right Side View

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

Example:

Input: [1,2,3,null,5,null,4]
Output: [1, 3, 4]
Explanation:

   1            <---
 /   \
2     3         <---
 \     \
  5     4       <---

Rust Solution

struct Solution;
use rustgym_util::*;

trait Preorder {
    fn preorder(&self, level: usize, view: &mut Vec<i32>);
}

impl Preorder for TreeLink {
    fn preorder(&self, level: usize, view: &mut Vec<i32>) {
        if let Some(node) = self {
            let node = node.borrow();
            let val = node.val;
            if level == view.len() {
                view.push(val);
            } else {
                view[level] = val;
            }
            node.left.preorder(level + 1, view);
            node.right.preorder(level + 1, view);
        }
    }
}

impl Solution {
    fn right_side_view(root: TreeLink) -> Vec<i32> {
        let mut res: Vec<i32> = vec![];
        root.preorder(0, &mut res);
        res
    }
}

#[test]
fn test() {
    let root = tree!(1, tree!(2, None, tree!(5)), tree!(3, None, tree!(4)));
    let res = vec![1, 3, 4];
    assert_eq!(Solution::right_side_view(root), res);
}

Having problems with this solution? Click here to submit an issue on github.