Problem 451632 · medium · Level 04 Non-Linear Data Structures

Weighing the Bottom Row

binary trees · breadth-first search · level tracking

Find the lowest row of a binary tree, the level furthest from the root (every node there is a leaf), and return the total of the values on that level. An empty tree gives 0.

Examples

          5
        /   \
       2     8
      /     / \
     6     1   4
      \   /
       9 3

Input:  root = build_tree([5, 2, 8, 6, None, 1, 4, None, 9, 3])
Output: 12
Explanation: the deepest level holds 9 and 3.
Input:  root = build_tree([4, 7, 2])
Output: 9

Constraints

  • 0 <= number of nodes <= 2000
  • -10**4 <= node.val <= 10**4
  • Target complexity: O(n) time.

Goals

  • Identify the last level of a level-order traversal
  • Sum a level while it is being processed
  • Handle chains where the deepest level has one node
Starting Python…