15% off all plans & roadmaps Use code
See plansLeetCode 1161 · Solution & editorial
Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on.
Maximum Level Sum of a Binary Tree is a Tree Traversal Patterns (DFS & BFS) problem, in the Level Order Traversal subpattern. The pattern covers preorder, inorder, level order traversal, LCA, and serialization
Master level order traversal using BFS. Solve zigzag, right side view, and level sum problems.
The reference solution runs in O(n) time and O(h) space.
Maximum Level Sum of a Binary Tree has been reported in interviews at Amazon, Facebook.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Maximum Level Sum of a Binary Tree is a Tree Traversal Patterns (DFS & BFS) problem, in the Level Order Traversal subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.