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.
For example:
Given the following binary tree,
1 <—
/ \
2 3 <—
\ \
5 4 <—
You should return [1, 3, 4].
解法1: BFS, O(N)
这题也是考察BFS算法的一个变形,题意是要求每一层最右边的node。那么我们按层遍历,对于每一层只取第一个(最右边)的node的数值就可以了。
C++
Java