You are given the root
of a binary tree containing digits from 0
to 9
only.
Each root-to-leaf path in the tree represents a number.
1 -> 2 -> 3
represents the number 123
.Return the total sum of all root-to-leaf numbers. Test cases are generated so that the answer will fit in a 32-bit integer.
A leaf node is a node with no children.
Example 1:
Input: root = [1,2,3] Output: 25 Explanation: The root-to-leaf path1->2
represents the number12
. The root-to-leaf path1->3
represents the number13
. Therefore, sum = 12 + 13 =25
.
Example 2:
Input: root = [4,9,0,5,1] Output: 1026 Explanation: The root-to-leaf path4->9->5
represents the number 495. The root-to-leaf path4->9->1
represents the number 491. The root-to-leaf path4->0
represents the number 40. Therefore, sum = 495 + 491 + 40 =1026
.
Constraints:
[1, 1000]
.0 <= Node.val <= 9
10
.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy involves exploring every single path from the top of the tree to the bottom. For each path, we'll construct a number and then add up all the numbers to get our final answer. It's like trying every route in a maze to find all the possible end points.
Here's how the algorithm would work step-by-step:
def sum_root_to_leaf(root):
total_sum = 0
def calculate_path_sum(node, current_number):
nonlocal total_sum
if not node:
return
# Build the number as we traverse down the tree
current_number = current_number * 10 + node.val
if not node.left and not node.right:
# Reached a leaf node, add current number to the total
total_sum += current_number
return
calculate_path_sum(node.left, current_number)
calculate_path_sum(node.right, current_number)
calculate_path_sum(root, 0)
return total_sum
We want to find the total value of numbers formed by paths from the root of a tree down to each leaf. The clever approach involves building up each number step-by-step as we travel down the tree and adding that number to a running total when we hit a leaf.
Here's how the algorithm would work step-by-step:
def sum_numbers(root):
total_sum = 0
def calculate_sum(node, current_number):
nonlocal total_sum
if not node:
return
# Build number as we traverse.
current_number = current_number * 10 + node.val
# Leaf node condition
if not node.left and not node.right:
total_sum += current_number
return
# Explore left subree.
calculate_sum(node.left, current_number)
# Explore right subtree.
calculate_sum(node.right, current_number)
calculate_sum(root, 0)
# Return accumulated sum
return total_sum
Case | How to Handle |
---|---|
Null root node | Return 0 if the root is null, as there's no path and thus no sum. |
Single node tree (root only) | Return the value of the root node itself since it's both the root and the leaf. |
Tree with very large depth, potentially causing stack overflow with recursion | Consider iterative DFS or BFS to avoid stack overflow errors from excessive recursion depth. |
Tree with very large numbers, potentially leading to integer overflow when concatenating the path number | Use a larger data type (e.g., long) to store the number, or use modular arithmetic to prevent overflow. |
Tree with all nodes having the value 0 | The algorithm should correctly sum the paths formed by all zeros (e.g., a path of 0 -> 0 should result in the number 0). |
Unbalanced tree with highly skewed distribution of node values | The algorithm should handle unbalanced trees efficiently; the performance will be affected by the tree's height, but the correctness will be preserved. |
Tree containing leading zeros in a path (e.g., 0 -> 1) | The standard approach of building the number concatenating from root to leaf will correctly form the number (e.g., path 0->1 will create the number 1 and not require any special handling). |
Nodes with negative values (invalid in the problem context but good to clarify) | Clarify with the interviewer whether negative values are allowed, and if so, how to handle them, as the problem description implies non-negative single digit values for each node. |