FunctionRacerProblemsRace

Level With Maximum Sum

EasyTreesPython, JavaScript, TypeScript, Java, C++

You are given the root of a binary tree. Add up the values on each level. The root is level 1, its children level 2, and so on. Return the number of the level with the largest sum; if several levels tie, return the smallest level number. The tree has at least one node.

Trees appear in the tests in level order, with null for a missing child, like [3,9,20,null,null,15,7]. Each node is a TreeNode with .val, .left and .right; the class is already defined for you.

Example 1

root [1,7,0,7,-8,null,null]
returns 2

Sums: 1, 7, -1. Level 2 is largest.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Trees problems