Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Maximum Level Sum of a Binary Tree (medium)
On this page

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!

On this page