40 / 75
Use BFS to traverse level by level, collecting the rightmost node at each level.
Example:
Input:
root=[1,2,3,null,5,null,4]Output:
[1,3,4] (rightmost at each level)Common Mistakes:
- Only traversing right subtree (misses cases where left is visible)
- Not handling levels properly (missing rightmost at each level)
- Confusing with pre-order traversal
- Not considering left children when right is null
Notes:
BFS approach shown. Can also solve with DFS (right-first traversal, track max depth). Time O(n), Space O(n).
💻
Java Solution Hidden
Enable “Show Full Solution” to view the code
Use BFS to traverse level by level, collecting the rightmost node at each level.
Example:
Input:
root=[1,2,3,null,5,null,4]Output:
[1,3,4] (rightmost at each level)Common Mistakes:
- Only traversing right subtree (misses cases where left is visible)
- Not handling levels properly (missing rightmost at each level)
- Confusing with pre-order traversal
- Not considering left children when right is null
Notes:
BFS approach shown. Can also solve with DFS (right-first traversal, track max depth). Time O(n), Space O(n).
40/75
Binary Tree Right Side View