You are given the root of a binary tree, root. Return the value of its deepest node, where the root has depth 0.
If more than one node is at the maximum depth, return the leftmost one: the node that comes first when that depth's nodes are listed left to right, in the same order as a level-order traversal.
Tree Encoding
Your function receives root as a TreeNode. The node type is provided for you, with val, left, and right fields.
The examples below write the tree as its level-order traversal, where null marks a missing child of a listed node and trailing nulls are omitted. That is only how the input is displayed; the decoding is done for you.
Constraints
The number of nodes in root is in the range [1,105].
−109≤Node.val≤109
Examples
Example 1
Input
root = [1, 2, 3, 4, 5, null, 6]
root
Output
4
Explanation
The deepest level is depth 2, holding `4`, `5`, and `6` in left-to-right order. The leftmost of them is `4`.
Example 2
Input
root = [1, 2, 3, null, null, 4, 5]
root
Output
4
Explanation
Node `2` has no children, so the deepest level (depth 2) only comes from `3`'s subtree: `4` and `5`. The leftmost is `4`.
Example 3
Input
root = [1]
root
Output
1
Explanation
The single node `1` is its own deepest node.
Loading editor...
Run checks the sample cases; Submit runs every case.
Samples 3
Custom 0
passedwrong answertime limiterrorran, no expected valuenot run