Acrobats From the Wings
A circus troupe builds a human tower: each acrobat can hold up to two others, one per shoulder. It is recorded as a binary tree of the acrobats' numbers. The floor acrobat is the root, and a node's children stand on its shoulders.
The stage manager watches from the right-hand wing and sees only the acrobat nearest that wing on each row. The lighting cue sheet needs exactly those numbers.
Your task
Write seenFromWing(root). For each level of the tree, take the rightmost node. Return their values starting at the root's level (the floor) and moving up one row at a time.
An empty tree gives an empty list. Trees are shown in level order, like [7, 3, 9, 1, 5], with null marking a missing child. Your function receives real TreeNode objects with val, left and right.
seenFromWing(root: TreeNode) → int[]
Examples
root = [7, 3, 9, 1, 5, null, null, null, null, 4]
[7, 9, 5, 4]
The rows are [7], [3, 9], [1, 5] and [4]. Acrobats 1 and 5 both stand on 3's shoulders, and 9 holds nobody. The rightmost acrobat of each row is 7, 9, 5 and 4. Following right shoulders alone would stop after 7 and 9.
root = [1, 2, 3, 4]
[1, 3, 4]
Row 3 holds only acrobat 4, on the far left, but nobody stands to its right, so the manager sees it.
Constraints
- 0 ≤ number of acrobats ≤ 1,000
- -100,000 ≤ number worn ≤ 100,000
- The tree is at most 500 levels deep.
Start with brute force
The quick guess walks down right shoulders only: root, root.right, root.right.right. It fails when the tower's right side is shorter than its left, as in the first example.
A correct but slow fix walks down from the root again for each row number to collect that row. Up to 500 fresh walks over 1,000 acrobats repeat a lot of work; one level-by-level pass finds every row at once.
Hints
0 of 4 shown- Hint 1
In the first example, the visible acrobat on row 3 stands on the left acrobat of row 2. So what decides visibility: the position under the parent, or the whole row?
- Hint 2
Visibility depends only on the row. Given one row in left-to-right order, the answer for it is the last acrobat.
- Hint 3
Which traversal hands you the tree one complete row at a time, left to right?
- Hint 4
Breadth-first search with a queue. At the start of each level, note the queue's size. Process exactly that many nodes, pushing left child then right child, and record the last value.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
The question is about rows, so walk the tree row by row, left to right, and take each row's last node.
Trace [7, 3, 9, 1, 5, null, null, null, null, 4]:
- Row [7]. Last is 7. Next row: 7's children, [3, 9].
- Row [3, 9]. Last is 9. Next row: 3 gives 1 and 5, 9 gives nothing, so [1, 5].
- Row [1, 5]. Last is 5. Next row: only 5 has a child, so [4].
- Row [4]. Last is 4. No children, so stop. Answer
[7, 9, 5, 4].
Pushing the left child before the right keeps each row in left-to-right order. If you push the right child first instead, the last node of each row is the leftmost one, which is the view from the other wing.
function seenFromWing(root) {
const seen = [];
if (root === null) return seen;
let row = [root];
while (row.length > 0) {
seen.push(row[row.length - 1].val); // rightmost on this row
const next = [];
for (const node of row) { // build the row above, left to right
if (node.left) next.push(node.left);
if (node.right) next.push(node.right);
}
row = next;
}
return seen;
}from collections import deque
def seen_from_wing(root):
seen = []
if root is None:
return seen
queue = deque([root])
while queue:
size = len(queue) # nodes on this row
for i in range(size):
node = queue.popleft()
if i == size - 1: # last one is nearest the wing
seen.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return seenRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.