#199 Binary Tree Right Side View
frequently reported atMetaOracleAmazonByteDance / TikTokUberBloomberg
the problem
In plain words
Standing on the right of the tree, list the values you can see from top to bottom — the last node of each level.
in root = [1,2,3,null,5,null,4]
out [1,3,4]
in root = [1,2,3,4,null,null,null,5]
out [1,3,4,5]
Levels 2 and 3 only have nodes on the left side — you still see them.
in root = [1,null,3]
out [1,3]
in root = []
out []
- 0 ≤ number of nodes ≤ 100
- -100 ≤ Node.val ≤ 100
your turn
Try it before you peek
Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.
Solve it in the browser
LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.
Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.
Marking the problem solved, or opening every hint, also shows it.
the aha
Key insight
patternTree BFS (level order)Tree DFS (top-down)
"Visible from the right" = the rightmost node of each level, which may well be in the left subtree. Think levels, not "follow the right pointers".
solutions
From brute force to optimal
Level-order traversal; record the node polled at index size − 1.
- Standard BFS with a size snapshot.
- When i == size − 1, add the value.
Every node visited once.
Queue width.
class Solution { public List<Integer> rightSideView(TreeNode root) { List<Integer> view = new ArrayList<>(); if (root == null) return view; Deque<TreeNode> queue = new ArrayDeque<>(); queue.offer(root); while (!queue.isEmpty()) { int size = queue.size(); // nodes on THIS level for (int i = 0; i < size; i++) { TreeNode node = queue.poll(); if (i == size - 1) view.add(node.val); // last of the level = visible if (node.left != null) queue.offer(node.left); if (node.right != null) queue.offer(node.right); } } return view; }}Dry run
Visit right before left; the first node reached at each depth is the visible one.
- walk(node, depth).
- If depth == view.size(), add node.val.
- Recurse right, then left.
Every node visited once.
Recursion stack.
class Solution { public List<Integer> rightSideView(TreeNode root) { List<Integer> view = new ArrayList<>(); walk(root, 0, view); return view; } private void walk(TreeNode node, int depth, List<Integer> view) { if (node == null) return; if (depth == view.size()) view.add(node.val); // first node seen at this depth wins walk(node.right, depth + 1, view); // right first! walk(node.left, depth + 1, view); }}in the room
How to say it in the interview
talk track
- Draw example 2 first: it shows why following right pointers is wrong.
- Present BFS (last of each level), then the DFS trick: right child first, first node per depth wins.
traps
- Only walking
root.rightpointers — misses levels where the rightmost node lives in the left subtree.
what they ask next
Follow-ups
Left side view?
Take i == 0 in BFS, or recurse left first in DFS.
Bottom view or top view?
Give each node a column (left = col − 1, right = col + 1) and keep the first (top) or last (bottom) node per column in a TreeMap, using BFS so the shallowest node is met first.
before you submit
Edge cases to test
- Empty tree → [].
- A left chain: you see every node — nothing hides them on the right.
- Right subtree shorter than the left: the deeper levels are seen through the left subtree.
practise next
Similar problems
- #102Binary Tree Level Order TraversalMediumin Traversals
- #117Populating Next Right Pointers in Each Node IIMediumin Traversals
- #104Maximum Depth of Binary TreeEasyin Tree Recursion