All problems

Problem 404

Bottom Left Tree Value

Medium · 20 pts · Tree, Breadth-First Search, Binary Tree

Problem

Find the leftmost value in the last (deepest) row of a binary tree. Note that this is not necessarily a left child: if the deepest row has a single node that is a right child, that node is the answer. A breadth-first traversal can record the first node of each level; the first node of the final level is the answer.

Input

The first line contains N, the number of tokens. The second line contains N tokens describing the tree in level order (breadth-first, left to right), where 'null' marks a missing child, as on LeetCode.

Output

Print the leftmost value of the deepest row.

Constraints

1 <= number of nodes <= 10000; -1000 <= value <= 1000

Example input

10
1 2 3 4 null 5 6 null null 7

Example output

7

Explanation

In the tree 1 / 2 3 / 4 _ 5 6 / _ _ 7, the deepest row contains only 7, so the answer is 7.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Count Leaf Nodes Next: Check Completeness of a Binary Tree