← DiffPush

Bottom View of Binary Tree

Standard Bar Binary Trees · Medium Problems O(n) · O(n)

The Underfloor Pipe Ledger

A plant inspector maps a tree-shaped sprinkler network from beneath the floor: each junction's sub-pipes fan one column left and one column right, and the lowest pipe in a column covers everything above it. The report lists one junction per column, left to right — and when two junctions occupy the same column at equal depth, the one reached later in a level sweep takes the ledger slot.

Input: The root of a binary tree given as a level-order array where null marks a missing child. The root sits at column 0.

Output: Return an array of bottom-view values, one per column, ordered left to right.

Constraints

Examples

Example 1

Input: {"tree":[1,2,3,4,5,6,7]}
Output: [4,2,6,3,7]
Column 0's deepest pair are 5 and 6; the later-swept 6 holds the slot.

Example 2

Input: {"tree":[1,2,3,null,4,null,5]}
Output: [2,4,3,5]
Deeper nodes 4 and 5 cover the root's column and column 2 respectively.

Solve this in your browser →

Also on LeetCode ↗