Sum the values of the deepest leaves in a binary tree

Quick Overview

Given the root of a binary tree, return the sum of the values of its deepest leaves, the leaf nodes that sit at the maximum depth. It tests tree traversal, tracking depth while visiting nodes, and keeping the work linear in the number of nodes.

Sum the values of the deepest leaves in a binary tree

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given the `root` of a binary tree, return the sum of values of its **deepest leaves**. A leaf is a node with no children. The deepest leaves are the leaf nodes at the maximum depth of the tree. ### Function Signature ```python def deepest_leaves_sum(root: Optional[TreeNode]) -> int: ``` ### Rules - `TreeNode` has the fields `val`, `left` and `right`. A missing child is `None`. - The examples write the tree as a level-order list. For each node that exists, its left and right children are listed in order, `None` marks a missing child, and trailing `None` values are left out. - Depth counts nodes from the root, so the root is at depth 1. ### Constraints - The number of nodes in the tree is in the range `[1, 10^4]`. - `1 <= Node.val <= 100` ### Examples ```text Input: root = [1,2,3,4,5,None,6,7,None,None,None,None,8] Output: 15 ``` Node 4 has a left child 7 and node 6 has a right child 8, and no other node reaches depth 4. The deepest leaves are 7 and 8, so the sum is 7 + 8 = 15 (leaf 5 sits at depth 3 and does not count). ```text Input: root = [6,7,8,2,7,1,3,9,None,1,4,None,None,None,5] Output: 19 ``` Depth 4 holds 9 (left child of 2), 1 and 4 (children of the second 7), and 5 (right child of 3). Their sum is 9 + 1 + 4 + 5 = 19. ```text Input: root = [5] Output: 5 ``` The root has no children, so it is the only leaf and also the deepest one.

Overview: Given the root of a binary tree, return the sum of the values of its deepest leaves, the leaf nodes that sit at the maximum depth. It tests tree traversal, tracking depth while visiting nodes, and keeping the work linear in the number of nodes.

Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given the root of a binary tree, return the sum of values of its deepest leaves.

A leaf is a node with no children. The deepest leaves are the leaf nodes at the maximum depth of the tree.

Function Signature

def deepest_leaves_sum(root: Optional[TreeNode]) -> int:

Rules

  • TreeNode has the fields val , left and right . A missing child is None .
  • The examples write the tree as a level-order list. For each node that exists, its left and right children are listed in order, None marks a missing child, and trailing None values are left out.
  • Depth counts nodes from the root, so the root is at depth 1.

Constraints

  • The number of nodes in the tree is in the range [1, 10^4] .
  • 1 <= Node.val <= 100

Examples

Input: root = [1,2,3,4,5,None,6,7,None,None,None,None,8]
Output: 15

Node 4 has a left child 7 and node 6 has a right child 8, and no other node reaches depth 4. The deepest leaves are 7 and 8, so the sum is 7 + 8 = 15 (leaf 5 sits at depth 3 and does not count).

Input: root = [6,7,8,2,7,1,3,9,None,1,4,None,None,None,5]
Output: 19

Depth 4 holds 9 (left child of 2), 1 and 4 (children of the second 7), and 5 (right child of 3). Their sum is 9 + 1 + 4 + 5 = 19.

Input: root = [5]
Output: 5

The root has no children, so it is the only leaf and also the deepest one.