199. Binary Tree Right Side View
Asked at TikTok
Difficulty
Medium
Time
O(n)
Space
O(w)
Pattern
Tree
Companies
1
Problem
You are given the root of a binary tree. Imagine you stand far to the right of the tree and look at it. Some nodes are hidden behind other nodes. Return the values of the nodes you can see, ordered from the top level down to the bottom level.
On each level of the tree, exactly one node is visible from the right: the rightmost node of that level. So the answer has one value per level, and the answer length equals the height of the tree.
Be careful: the rightmost node of a level is not always in the right subtree of the root. If the right subtree is short, the visible node at deeper levels comes from the left subtree. If the tree is empty, return an empty list.
What you see from the right
Constraints
- ●
The number of nodes in the tree is in the range [0, 100]. - ●
-100 <= Node.val <= 100
Examples
root = [1,2,3,null,5,null,4][1,3,4]root = [1,2,3,4,null,null,null,5][1,3,4,5]root = [1,null,3][1,3]root = [][]Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| TikTok | Medium | View all TikTok questions → |
How to Think About It
The rightmost node on a level is not always in the right subtree. If the right subtree is shallow, the view at deeper levels comes from the left subtree.
BFS approach: process the tree level by level. For each level, the last node dequeued is the one visible from the right.
DFS approach: visit right child before left child and carry the depth. The first time you reach a new depth (depth == len(result)), that node is the rightmost one at that depth.
Walkthrough for [1,2,3,null,5,null,4]: level 0 -> [1] sees 1; level 1 -> [2,3] sees 3; level 2 -> [5,4] sees 4. Result [1,3,4].
Edge cases: empty tree returns []; a left-only chain returns every node.
Optimal Approach
Step 1: If root is null, return [].
Step 2: Put root in a queue.
Step 3: While the queue is not empty:
size = len(queue)
Pop size nodes; push their non-null children left then right.
The last popped node of this level is visible — append its value.
Step 4: Return the result.
Every node is enqueued and dequeued once.
Time: O(n). Space: O(w) where w is the maximum width of the tree.
BFS level by level
What Trips People Up in Real Interviews
Only walking right pointers from the root. That misses nodes that are visible because the right subtree is shorter than the left.
Not snapshotting the queue size before the inner loop. Without it you mix levels and cannot tell which node is last on a level.
In the DFS version, visiting left before right. Then the first node you see at each depth is the leftmost, giving the left side view.
Forgetting the empty-tree case and dereferencing a null root.
Solution Code
O(n) timefrom collections import deque
def rightSideView(root):
if not root:
return []
res = []
q = deque([root])
while q:
size = len(q)
for i in range(size):
node = q.popleft()
if i == size - 1:
res.append(node.val)
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
return resFrequently Asked Questions
What is the Binary Tree Right Side View problem?
Binary Tree Right Side View asks for the values you would see standing to the right of a binary tree, top to bottom — in other words, the last node on each level. It is a level-order traversal question with a small twist, and a common follow-up after Binary Tree Level Order Traversal.
How do you solve Binary Tree Right Side View?
The optimal approach runs in O(n) time and O(w) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Binary Tree Right Side View?
Binary Tree Right Side View is asked at TikTok. It is a medium difficulty problem.
What are common mistakes on Binary Tree Right Side View?
- Only walking right pointers from the root. That misses nodes that are visible because the right subtree is shorter than the left.
- Not snapshotting the queue size before the inner loop. Without it you mix levels and cannot tell which node is last on a level.
- In the DFS version, visiting left before right. Then the first node you see at each depth is the leftmost, giving the left side view.
- Forgetting the empty-tree case and dereferencing a null root.