102. Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

For example:
Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its level order traversal as:

[
  [3],
  [9,20],
  [15,7]
]

Rust Solution

struct Solution;
use rustgym_util::*;

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

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

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

#[test]
fn test() {
    let root = tree!(3, tree!(9), tree!(20, tree!(15), tree!(7)));
    let res: Vec<Vec<i32>> = vec_vec_i32![[3], [9, 20], [15, 7]];
    assert_eq!(Solution::level_order(root), res);
}

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