Binary Tree Right Side View
Binary Tree Right Side View is LeetCode 199 (Medium). Imagine standing to the right of a binary tree: return the values of the nodes you can see, from top to bottom.
- You see exactly one node per level, the rightmost node on that level.
- The tree can be empty; then the answer is
[].
With at most 100 nodes, a single O(n) walk is all that is needed.
- The number of nodes in the tree is in the range [0, 100].
- -100 <= Node.val <= 100
Intuition
Seeing one node per level does not mean following right children down. When the left subtree is deeper, its lower nodes stick out below the right side and are visible too.
So think per depth. A depth-first search that visits the right child before the left, carrying the depth, meets each node in one of two situations:
- First node at its depth: everything to its right at that depth was searched earlier and nothing was there, so it is the visible one.
- A later node at that depth: a node further right already covers it, so it is hidden.
The answer list tells the two apart: its length is the number of depths already seen.
Any question that wants one node per level, such as the right view, the left view, the largest value in each row or the bottom-left value, can be solved two ways: a BFS that handles one level at a time, or a DFS that carries the depth and compares it with the length of the answer.
Approach
Before reading on: in [1,2,3,4], node 3 has no children but node 2 has a child 4. Which nodes can you see from the right, and why is 4 one of them even though it sits on the left side?
Two ways to solve it
Visit right before left and keep the first node reached at each depth.
- Memory: one root-to-node path, small on a balanced tree.
- Code: a few lines; the answer length tracks the depths.
- Limit: a very deep chain can hit the recursion limit.
The shortest answer for interviews.
Handle the tree one level at a time and keep the last node of each.
- Memory: the queue holds a whole level, up to n/2 nodes.
- Depth: no recursion, safe on a long chain.
- Reuse: the same loop as level order traversal.
Good when you think in levels.
Both visit each node once. The DFS keeps only one path in memory instead of a whole level and is shorter to write, so the steps, code and live demo follow it; the BFS code comes after the demo.
Visit with the depth
Call dfs(node, depth) starting with (root, 0); an empty node returns at once. The depth is an argument, so each call knows its own level and nothing has to be reset when a call returns.
Record the first node at each depth
If depth == len(view), no node at this depth has been recorded yet, so append node.val. Otherwise this depth already has its visible node, and the current one is hidden behind it.
Go right, then left
Recurse into node.right with depth + 1, then into node.left. Right first makes the first arrival at each depth the rightmost node there. The left subtree is still searched, because it can reach depths the right side never does.
Code in Python, C++ and Java
Level-by-level BFS
A queue holds one level at a time, left to right. Before a level is processed, the last node in the queue is the rightmost on that level, so its value goes into the answer; then the level's nodes are dequeued and their children enqueued.
Common pitfalls
Following only the right children
node = root
while node:
view.append(node.val)
node = node.rightdfs(node.right, depth + 1) dfs(node.left, depth + 1)
Nodes of a deeper left subtree are visible too. For [1,2,3,4] this returns [1, 3], but 4 is visible below 3, so the answer is [1, 3, 4].
Recursing left before right
dfs(node.left, depth + 1) dfs(node.right, depth + 1)
dfs(node.right, depth + 1) dfs(node.left, depth + 1)
The first node reached at each depth is then the leftmost one, so the code returns the left side view. For [1,2,3] it gives [1, 2] instead of [1, 3].
Complexity
Binary Tree Right Side View FAQ
Is GFG Right View of Binary Tree the same problem?
Yes. The GeeksforGeeks version asks for the same nodes in the same top-to-bottom order, so this code works there unchanged.
How do you get the left view instead?
Visit the left child before the right one; the first node at each depth is then the leftmost. In the BFS version, record the first node of each level instead of the last.