Binary Tree Right Side View
Return the node you would see on each level when looking at a tree from its right side, using BFS.
The problem
You get the root of a binary tree. Imagine standing to the right of it: on each level you can only see the rightmost node. Return the values you would see, from the top level to the bottom one.
Input root = [5, 3, 8, 1, 4, null, null, 0]
Output [5, 8, 4, 0]
On the third level 4 is rightmost, and on the last level the only node, 0, is visible even though it is a left child.
Input root = [2, 4, 6, 8, 10, 12, 14]
Output [2, 6, 14]
In a full tree the rightmost node of each level is simply the last one.
Key constraint: The rightmost node of a level can sit in the left subtree whenever the right subtree is shorter.
How to spot the pattern
- You need one node per level: the last one when you read the level from left to right.
- A node from the left subtree can be visible when the right subtree is shorter, so following right children alone fails.
The idea
Process the tree level by level with BFS. Within a level, nodes leave the queue in left-to-right order, so the last node you pop on each level is the one you see from the right. Record its value, and push children as usual.
Brute force: For every depth, search the whole tree for the rightmost node at that depth, which costs O(n * h) time.
Step by step
- 1If the root is null, return an empty list; otherwise put the root in a queue.
- 2For each level, read the queue's size
sizeand pop that many nodes. - 3When you pop the last node of the level (number
size - 1), add its value to the answer. - 4Push each popped node's left and right children when they exist, and return the answer at the end.
See the pattern move
The tree bfs pattern on a small example. Step through it, or press play.
Code
from collections import deque
class Solution:
def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
if root is None:
return []
view = []
queue = deque([root])
while queue:
size = len(queue)
for i in range(size):
node = queue.popleft()
if i == size - 1: # last node of the level: visible from the right
view.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return viewComplexity
Time
O(n)
Space
O(w)
Each node is pushed and popped once; the queue holds at most about one level, w nodes, plus the answer's one value per level.
Watch out for
- Following only right children: in [5, 3, 8, 1, 4, null, null, 0] that misses 4 and 0, which you can see because the right side is shorter.
- Taking the first node of each level instead of the last, which gives the left side view.
Follow-up questions
Can you do it with DFS?
Visit the right child before the left one and pass the depth down; the first node you reach at each new depth is the visible one, so append it when depth equals the answer's length.
How would you get the left side view?
Take the first node of each level in the BFS, or visit the left child first in the DFS version.
In Hinglish
Har level ka sabse aakhri node hi right side se dikhta hai. BFS mein har level ke last node ki value answer mein daal do.