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 problem asks us to find the sum of all numbers formed by paths from the root to the leaves in a tree. The brute force approach will explore every possible path from the root to a leaf, calculating the number for each path, and finally summing all those numbers together.
Here's how the algorithm would work step-by-step:
def sum_root_to_leaf(root):
def dfs(node, current_number):
if not node:
return 0
current_number = current_number * 10 + node.val
# If we are at a leaf node, add the current number to the total
if not node.left and not node.right:
return current_number
# Recursively explore the left and right subtrees
return dfs(node.left, current_number) + dfs(node.right, current_number)
return dfs(root, 0)
The core idea is to traverse the tree in a way that builds the number from the root to each leaf as we go. We keep track of the number formed so far and update it as we move down the tree.
Here's how the algorithm would work step-by-step:
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def sum_numbers(root):
def dfs(node, current_number):
if not node:
return 0
current_number = current_number * 10 + node.val
# If it's a leaf, add the number to the total.
if not node.left and not node.right:
return current_number
# Explore the left and right subtrees.
left_sum = dfs(node.left, current_number)
right_sum = dfs(node.right, current_number)
# Summing up the paths from left and right
return left_sum + right_sum
# Initiate the depth-first search from the root.
return dfs(root, 0)
Case | How to Handle |
---|---|
Null root node | Return 0 immediately since there are no root-to-leaf paths. |
Single node tree (root is also a leaf) | Return the value of the root node as it represents a single path. |
Tree with all nodes having value 0 | The algorithm should correctly compute the sum of all root-to-leaf numbers, which will be 0. |
Tree with very long paths leading to potential integer overflow | Use a data type that can handle larger numbers, or use modular arithmetic during path number construction. |
Tree where all left children are null | The algorithm should still correctly traverse and sum the root-to-leaf paths even with a skewed tree structure. |
Tree where all right children are null | The algorithm should still correctly traverse and sum the root-to-leaf paths even with a skewed tree structure. |
Deeply unbalanced tree causing stack overflow in recursive solutions | Consider an iterative approach (e.g., using a stack) instead of recursion to avoid exceeding stack limits. |
Nodes with non-digit values (outside 0-9 range) | Add validation to ensure nodes only contain digits and return an error or handle as 0 if invalid data is found. |