Ctrl K

Largest Value on Each Tree Level

Medium
You are given the root of a binary tree. A level is the set of nodes at the same distance from the root: the root is level 0, its children are level 1, and so on. Return a list whose i-th entry is the largest value among the nodes on level i. For an empty tree, return an empty list.
Example 1
root=tree_node([4, 9, 2, -3, None, 7, 1]) → [4, 9, 7]

Depth 0 holds only 4; depth 1 holds 9 and 2, so 9; depth 2 holds -3, 7 and 1, so 7.

Example 2
root=tree_node([-5, None, -8]) → [-5, -8]

Each level has a single node, so that node is the level's maximum.

Example 3
root=tree_node([]) → []
Constraints
  • The tree has between 0 and 10⁴ nodes
  • -2³¹ ≤ node value ≤ 2³¹ − 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsThe Tree Seen From the Right MediumTree Columns, Top Down MediumFirst Value on the Lowest Row MediumInsert a Tree Level MediumA Tree Drawn on a Grid Medium
loading Python…
Input
root=tree_node([4, 9, 2, -3, None, 7, 1])
Expected
[4, 9, 7]

Run checks the first 3 tests in your browser. Submit checks all 80 tests on the server, hidden ones included.