Sum Root to Leaf Numbers
MEDIUMDescription
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.
- For example, the root-to-leaf path
1 -> 2 -> 3represents the number123.
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->2represents the number12. The root-to-leaf path1->3represents 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->5represents the number 495. The root-to-leaf path4->9->1represents the number 491. The root-to-leaf path4->0represents the number 40. Therefore, sum = 495 + 491 + 40 =1026.
Constraints:
- The number of nodes in the tree is in the range
[1, 1000]. 0 <= Node.val <= 9- The depth of the tree will not exceed
10.
Approaches
Checkout 3 different approaches to solve Sum Root to Leaf Numbers. Click on different approaches to view the approach and algorithm in detail.
Brute Force: Generate All Paths and Sum
This approach first generates all root-to-leaf paths, stores them, and then iterates through these paths to calculate the corresponding numbers and their sum. It's straightforward but inefficient due to high space usage.
Algorithm
- Initialize an empty list,
allPaths, to store string representations of the numbers for each root-to-leaf path. - Define a recursive helper function
findPaths(node, currentPathString, allPaths). - In the helper function, if the current
nodeis null, return. - Append the current node's value to
currentPathString. - If the current
nodeis a leaf (both left and right children are null), add thecurrentPathStringto theallPathslist. - Recursively call the helper function for the left and right children.
- After the initial call to
findPathson the root completes, iterate throughallPaths. - For each string in
allPaths, convert it to an integer and add it to atotalSum. - Return
totalSum.
The core idea is to separate the problem into two distinct steps: path finding and summation.
-
Path Finding: A Depth-First Search (DFS) traversal is used to find all paths from the root to every leaf node. We use a helper function that maintains the current path from the root. When a leaf is encountered, the current path is complete and is added to a list of all paths.
-
Summation: After the traversal is complete, we have a list of all paths (where each path is a list of digits). We iterate through this list. For each path, we convert the sequence of digits into an integer. For example, the path
[4, 9, 5]is converted to the number 495. These numbers are then added to a running total.
Here is a code snippet illustrating this approach:
import java.util.ArrayList;
import java.util.List;
class Solution {
public int sumNumbers(TreeNode root) {
List<String> allPaths = new ArrayList<>();
findPaths(root, "", allPaths);
int totalSum = 0;
for (String path : allPaths) {
totalSum += Integer.parseInt(path);
}
return totalSum;
}
private void findPaths(TreeNode node, String currentPath, List<String> allPaths) {
if (node == null) {
return;
}
currentPath += node.val;
if (node.left == null && node.right == null) {
allPaths.add(currentPath);
return;
}
findPaths(node.left, currentPath, allPaths);
findPaths(node.right, currentPath, allPaths);
}
}
Complexity Analysis
Pros and Cons
- The logic is separated into two clear, understandable steps: finding paths and then summing them.
- It's a direct translation of the problem statement.
- Very inefficient in terms of space. It requires storing all root-to-leaf paths, which can be memory-intensive for large trees.
- The time complexity is also suboptimal due to the overhead of creating and storing path strings/lists and then iterating over them again.
Code Solutions
Checking out 4 solutions in different languages for Sum Root to Leaf Numbers. Click on different languages to view the code.
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public int sumNumbers ( TreeNode root ) { return dfs ( root , 0 ); } private int dfs ( TreeNode root , int s ) { if ( root == null ) { return 0 ; } s = s * 10 + root . val ; if ( root . left == null && root . right == null ) { return s ; } return dfs ( root . left , s ) + dfs ( root . right , s ); } }Video Solution
Watch the video walkthrough for Sum Root to Leaf Numbers
Similar Questions
5 related questions you might find useful
Algorithms:
Data Structures:
Companies:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.