LeetCode75-1161-Max-Level-Sum

🧠 Summary of Thoughts

Idea

To solve this problem, we use Breadth-First Search (BFS) to traverse the binary tree level by level. We maintain a queue to store nodes for BFS traversal. For each level, we calculate the sum of all node values and compare it with the current maximum sum. If the current level sum is greater than the maximum sum seen so far, we update the maximum sum and record the corresponding level number.

✅ Code(Java)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public int maxLevelSum(TreeNode root) {
int maxSum = Integer.MIN_VALUE;
int level = 1;
int resLev = 1;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
int sum = 0;
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
sum += node.val;
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
if (sum > maxSum) {
maxSum = sum;
resLev = level;
}
level++;
}
return resLev;
}
}

LeetCode75-1161-Max-Level-Sum
https://powersixxx.github.io/leetcode-blog/2026/03/07/LeetCode75-1161-Max-Level-Sum/
作者
Baowen Liu
发布于
2026年3月7日
许可协议