Binary Trees Breadth first: level by level
368. Binary Tree Right Side View
You are given root, the root node of a binary tree. Picture yourself standing to the right of the tree and
looking at it. On each level you see only one node, the one furthest to the right, and it hides the others.
Return the values of the nodes you see, from the top level down.
The root is the only node on the first level. Its children make up the second level, their children the
third, and so on. Nodes on the same level can have different parents. An empty tree shows nothing, so the
answer is [].
The examples write a tree level by level, from left to right, with null where a child is missing. So
root = [5,8,null,3] is the root 5 with a left child 8 and no right child, and 8 has a left child 3. The
missing children after the last node are left out.
Example 1
Input: root = [10, 4, 6, 3, null, null, null, 8]
[10,6,3,8]Explanation: You see 10, then 6, which hides 4. The third and fourth levels hold one node each, 3 and 8. Both are on the left side of the tree, but nothing stands to their right, so you see them too.
Example 2
Input: root = []
[]Explanation: The tree has no nodes, so there is nothing to see.
Example 3
Input: root = [0, -1000000000, 1000000000, null, -7]
[0,1000000000,-7]Explanation: 1000000000 hides −1000000000 on the second level. On the third level, −7 is the only node.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
0 ≤ number of nodes ≤ 105-109 ≤ Node.val ≤ 109- The tree has at most 1000 levels.
Clarifying questions
What you would ask an interviewer before writing any code.