Largest Value in Each Tree Row
Problem statement
Given the root of a binary tree, return the largest node value at every depth, ordered from the root level downward.
Return an empty list when the tree is empty.
Function
largestValues(root: TreeNode) → List<Integer>Examples
Example 1
root = [1,3,2,5,3,null,9]return = [1,3,9]The maximum values at depths zero, one, and two are 1, 3, and 9.
Example 2
root = [1,2,3]return = [1,3]The root is the only value at depth zero, and 3 is the larger child at depth one.
Example 3
root = []return = []An empty tree has no rows.
Constraints
- The tree contains between
0and10000nodes. -2147483648 <= node.val <= 2147483647.