Grokking Tree Coding Patterns for Interviews
0% completed
Maximum Level Sum of a Binary Tree (medium)
Problem Statement
You are given the root of a binary tree. The level of its root node is 1, the level of its children is 2, and so on.
Return the level x where the sum of the values of all nodes is the highest. If there are multiple levels with the same maximum sum, return the smallest level number x.
Examples
Example 1:
- Input: root =
[1, 20, 3, 4, 5, null, 8]
- Expected Output:
2 - Explanation:
Level 1 has nodes:[1]with sum =1
Level 2 has nodes:[20, 3]with sum =20 + 3 = 23
Level 3 has nodes:[4, 5, 8]with sum =
.....
.....
.....
Like the course? Get enrolled and start learning!
Gwonsoo Lee
· 2 years ago
The post said that the expected output of example 3 should be 2, but it should be 3. The explanation is correct, but the expected output seems wrong.
Show 2 replies