Back to problems Sign in to unlock
Leetcode
Medium
Trees
Queues
Google
Maximum Level Sum Of A Binary Tree
Find the level of a binary tree with the largest sum of node values.
Acceptance 100%
Problem Statement
Problem
Given the root of a binary tree, compute the sum of values at each depth level. Return the level number that has the maximum sum.
If multiple levels have the same maximum sum, return the smallest level index.
The root is considered level 1.
Notes
- A level contains all nodes that are the same distance from the root.
- The tree may contain negative values, so the best level is not always the deepest one.
- You should return the earliest level when there is a tie.
Input Format
- A binary tree root node.
- Each node contains an integer value and optional left/right children.
Output Format
- Return a single integer: the 1-indexed level with the maximum sum.
Constraints
- The tree contains at least 1 node.
- Node values may be negative, zero, or positive.
- The tree size is finite and fits in memory.
- Use 1-indexed levels.
Examples
Sample cases returned by the problem API.
Example 1
Input
root = [1,7,0,7,-8,null,null]
Output
2
Explanation
Level 1 sum = 1. Level 2 sum = 7 + 0 = 7. Level 3 sum = 7 + (-8) = -1. The maximum sum is at level 2.
Example 2
Input
root = [1,-2,3,4,5,-6,2]
Output
3
Explanation
Level 1 sum = 1. Level 2 sum = 1. Level 3 sum = 5. The maximum sum is at level 3.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.
Guided hints
Editorial and discussion links
Concept map and variants
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.