Problem
You get the root of a binary tree. The input shows the tree as a list in level order. This order lists the nodes one level at a time, from left to right. A null value marks a missing child.
The root is at level 1. Its children are at level 2, their children are at level 3, and so on. The level sum is the sum of all node values at one level.
Return the level with the largest level sum. If two or more levels have the same largest sum, return the smallest of these levels.
Example 1
- Input:
- root = [2,9,-4,3,null,6,1]
- Output:
- 3
- Explanation:
- The level sums are 2, 5 and 10. Level 3 has the largest sum.
Example 2
- Input:
- root = [-5,-1,-3,null,-8]
- Output:
- 2
- Explanation:
- The level sums are -5, -4 and -8. The largest sum is -4, at level 2.
Example 3
- Input:
- root = [4,1,3]
- Output:
- 1
- Explanation:
- Level 1 and level 2 both have the sum 4. Level 1 is the smaller level.
Constraints
- 1 <= number of nodes <= 10^4
- -10^5 <= Node.val <= 10^5
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Binary Tree Maximum Path Sum
Coding · Binary Tree · Depth-First Search · DP on Trees
- Maximum Sum BST in Binary Tree
Coding
- Maximum Sum Leaf to Root Path in Binary Tree
Coding
- Binary Search Tree to Greater Sum Tree
Coding · Binary Search Tree · Binary Tree · Depth-First Search
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 1 record
No company association
Leetcode 1161. Maximum Level Sum of a Binary Tree
Reported date not specified · 0 source reports
Source snapshot: Nov 12, 2026
View source