Imagine you are standing on the right side of a binary tree. Which nodes would you be able to see? The Binary Tree Right Side View interview question asks you to return the values of these nodes in order from top to bottom. Essentially, for every level of the tree, you want to find the rightmost node.
This is a classic "level-based" problem asked by companies like Google, Meta, and Amazon. It tests your ability to modify a standard traversal to extract specific information. It can be solved using either BFS or DFS, making it a great way to show off your flexibility. It also tests your understanding of tree depth and level tracking.
There are two common ways to solve this:
Consider this tree:
1 <--- (1)
/
2 3 <--- (3)
\
5 4 <--- (4)
A common mistake is thinking you only need to follow the "right" pointers from the root. This is wrong because a node on the far left might be the only node at a very deep level, making it visible from the right side. Another mistake is using BFS but failing to identify which node is the "last" one in each level.
Try solving this with both BFS and DFS. The BFS approach is more intuitive for "level" problems, but the DFS approach is often more elegant and uses less extra space (aside from the recursion stack).
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Maximum Width of Binary Tree | Medium | Solve | |
| Find Largest Value in Each Tree Row | Medium | Solve | |
| Reverse Odd Levels of Binary Tree | Medium | Solve | |
| Add One Row to Tree | Medium | Solve | |
| Find Bottom Left Tree Value | Medium | Solve |