Leftmost desk on the lowest floor: hints, no solution

Medium · Trees

An office building is modelled as a binary tree: the root is the top of the hierarchy and every step down a link is one floor lower. Each node holds the number of a desk.

The tree is given in level order: the root first, then the children of each present node from left to right, with null standing for a missing child. Nodes below a missing child are not listed.

The lowest floor is the deepest level that contains at least one desk. Return the number of the leftmost desk on that floor, meaning the one that comes first when the floor is read from left to right. If the tree is empty, return -1.

Examples

Input
tree = [12, 7, 15, null, 9, 20, 3]
Output
9
Note
The deepest level holds 9, 20 and 3; 9 is the leftmost.
Input
tree = [5, 8, 4, 6]
Output
6
Note
The deepest level has only desk 6, on the left.

Constraints

  • 0 <= number of desks <= 2000
  • Desk numbers are integers from 1 to 100000 and may repeat

Hint 1: a nudge

Which desks share a floor? What would you need to know about a desk to tell how far down it is?

Unlock the full hints and timed practice with a pass

The second and third hints and the timed, runner-checked practice are part of a pass.

See pricing

Sign in

Check my resume first