Binary Tree Right Side View
List the nodes you see when you look at a tree from its right
DSA, Medium, Trees
Which structure or algorithm, and what does it cost.
You get a binary tree tree. Your code may take the root node or the list. Each node has val, left and right, null when missing. Either is fine; no conversion is needed. The examples use a level-order list. It holds values row by row, left to right. Null marks a missing child. A missing node has no entries below it. An empty list is an empty tree. Look at the tree row by row. In each row, take the node that has no node after it in that row. Return those values from the top row down.
Examples
Constraints
- 0 <= number of nodes <= 30,000.
- Each value is a whole number between -5,000 and 5,000.
- The tree can be one long chain.
Your answer must contain
- The simple method and its cost; the better method; the best method and why it is correct; code; time and memory cost; a hand trace of example 1.
Target Aim for O(n) time.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Think of the tree drawn in rows. In one row, which node comes last?
Hint 2 the approach
Which node of a row hides the others when you look from the right? How do you get the rows one after the other?
Hint 3 the key step
Hold the current row as a list. Save its last node's value, then build the next row from the children of its nodes. Stop when the row is empty.
Walkthrough
Full walkthrough brute force to best
Brute force
For each depth, walk the whole tree left to right and remember the last node seen at that depth. One walk per depth, and each walk visits every node. On a chain of n nodes that is n walks of n steps each, so O(n^2) time.
Better method
Walk depth first, visiting the right child before the left, and carry the depth. The first node reached at a new depth is the visible one for that row, so record it and skip later nodes of that depth. That costs O(n) time. The recursion goes as deep as the tree, so a long chain can overflow the call stack.
Best method
Build the tree row by row. The first row is the root. The next row is the children of the current row, kept in left-to-right order. The visible node of a row is its last one. This holds even when that node is a left child, because nothing in its row is further right. Save the last value of every row. Each node enters one row once, so the work is linear. There is no recursion, so a long chain is safe.
Hand trace
Run on [5, 3, 8, 1, 4, null, 9]. The tree: 5 has children 3 and 8. Node 3 has children 1 and 4. Node 8 has a right child 9. Row 1 is [5], the last value is 5. Row 2 is [3, 8], the last value is 8. Row 3 is [1, 4, 9], the last value is 9. Row 4 is empty, so we stop. The view is [5, 8, 9].
Code
def right_side_view(root):
view, row = [], [root] if root else []
while row:
view.append(row[-1].val)
row = [child for node in row for child in (node.left, node.right) if child]
return viewTime and memory
O(n) time, because every node is placed in one row and read once. O(w) extra memory holds the current row, with w the widest row.
Common mistakes
- Following only right children. When a row has no right child at some depth, the visible node is a left child, as in example 2.
- Taking the first node of each row. That gives the view from the left.
- Taking the largest value of each row. The visible node is the last one by position, not the biggest by value.