A building's structural drawing is a binary tree, one level of the tree per storey going downward. The foundation is the deepest level. Its cornerstone is the left-most node on that deepest level. Report the value written on that cornerstone.
Line 1: an integer n, the number of tokens on the next line.
Line 2: n space-separated tokens describing a binary tree in level-order (breadth-first). The first token is the root's value. Reading left to right, keep a queue of already-created nodes; for each node taken from the front of the queue, the next two tokens are its left child then its right child, where the token null marks a missing child. Only non-null children are added to the queue. Trailing null tokens for absent children at the deepest level may be omitted. Every node value is an integer.
A single integer: the value of the left-most node on the deepest level.
Example 1
Input
7 3 9 20 null null 15 7
Expected
15
Explanation
The deepest level holds 15 and 7; the left-most is 15.
Example 2
Input
7 1 2 3 null null 4 5
Expected
4
Explanation
The deepest level holds 4 and 5 (both under node 3); the left-most is 4.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →