Maximum Level Sum of a Binary Tree [Medium]

20 Sep 2026 2 min read
2
The Maximum Level Sum of a Binary Tree problem requires finding the level of a binary tree that has the maximum sum of node values.

Problem

Given the root of a binary tree, return the smallest level number that has the maximum sum of node values.

The root is considered to be at level 1. For each level, calculate the sum of all node values at that level and return the level with the highest sum. If multiple levels have the same sum, return the smallest level number.

Example(s)

Consider the following examples to understand the expected input and output.

Example 1

Input
root:
        1
       / \
      7   0
     / \   \
    7  -8   7
Output
2

Example 2

Input
root:
        989
       /   \
    10250   98693
    /   \      \
  100   -893   -321
Output
2

Solution

This solution uses Breadth-First Search (BFS) with a Queue to process the binary tree level by level.

At the beginning of each level, n stores the number of nodes currently in the queue. We process exactly those nodes and calculate their level sum using count. Their children are then added to the queue for the next level.

After processing each level, we compare its sum with the maximum sum found so far. If the current sum is greater, we update max and store the current level in maxLevel.
class Solution {
    public int maxLevelSum(TreeNode root) {
        int max = Integer.MIN_VALUE;
        int maxLevel = 0;

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        int level = 0;

        while (!queue.isEmpty()) {
            int n = queue.size();
            level++;
            int count = 0;

            for (int i = 0; i < n; i++) {
                TreeNode node = queue.poll();
                count += node.val;

                if (node.left != null) {
                    queue.offer(node.left);
                }

                if (node.right != null) {
                    queue.offer(node.right);
                }
            }

            if (count > max) {
                max = count;
                maxLevel = level;
            }
        }
        return maxLevel;
    }
}

Complexity

Each node is processed exactly once, so the time complexity is O(n), where n is the number of nodes in the binary tree.

The queue stores nodes from the tree levels being processed. In the worst case, it can contain O(n) nodes, so the extra space complexity is O(n).
Nagesh Chauhan

Nagesh Chauhan

Principal Software Engineer • Java • Python • Distributed Systems • AI/ML

Principal Software Engineer with 14+ years of experience designing and delivering large-scale distributed systems, cloud-native applications, and AI-powered platforms.

Passionate about solving complex engineering problems using strong data structures and algorithms, along with expertise in Java, Spring Boot, Python, System Design, Microservices, Cloud, Kafka, Elasticsearch, and Generative AI.

Share this Article

💬 Comments

Join the Discussion