Watch 10 video solutions for Sum of Root To Leaf Binary Numbers, a easy level problem involving Tree, Depth-First Search, Binary Tree. This walkthrough by Knowledge Center has 9,179 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given the root of a binary tree where each node has a value 0 or 1. Each root-to-leaf path represents a binary number starting with the most significant bit.
0 -> 1 -> 1 -> 0 -> 1, then this could represent 01101 in binary, which is 13.For all leaves in the tree, consider the numbers represented by the path from the root to that leaf. Return the sum of these numbers.
The test cases are generated so that the answer fits in a 32-bits integer.
Example 1:
Input: root = [1,0,1,0,1,0,1] Output: 22 Explanation: (100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
Example 2:
Input: root = [0] Output: 0
Constraints:
[1, 1000].Node.val is 0 or 1.Problem Overview: Each root-to-leaf path in the binary tree represents a binary number where every node contributes a bit (0 or 1). Moving from the root to a leaf builds a binary sequence. Convert each path into its decimal value and return the sum of all such numbers.
Approach 1: Recursive Depth-First Search (DFS) (Time: O(n), Space: O(h))
This approach traverses the tree using recursion. As you move down a path, maintain the current binary value formed so far. At each node, shift the accumulated value left by one bit (current << 1) and add the nodeβs value using a bitwise OR or addition. When a leaf node is reached, the accumulated value represents a full binary number, so add it to the total sum.
The key insight: binary digits are built incrementally as you traverse the path. Shifting left is equivalent to multiplying by 2, which matches how binary numbers grow when a new bit is appended. Every node is processed exactly once, making the time complexity O(n) where n is the number of nodes. Recursion depth depends on the tree height, so space complexity is O(h). This method naturally fits problems involving path-based computations in tree structures and is the cleanest implementation using Depth-First Search.
Approach 2: Iterative Depth-First Search with Stack (Time: O(n), Space: O(h))
The iterative version replaces recursion with an explicit stack. Each stack entry stores a pair: the current node and the binary value built along the path to that node. Pop a node from the stack, update the binary value using the same shift operation (value = (value << 1) | node.val), and check whether it is a leaf. If it is, add the value to the total sum. Otherwise, push its children onto the stack with the updated value.
This approach performs the same logical steps as recursive DFS but avoids relying on the call stack. It is useful in environments where recursion depth might be limited or when you want explicit control over traversal order. Time complexity remains O(n) since each node is visited once, and the stack holds at most O(h) nodes at a time in a balanced binary tree.
Recommended for interviews: Recursive DFS is typically expected because the code is concise and directly mirrors the tree structure. Interviewers look for the insight that the path forms a binary number and can be built incrementally using a left shift operation. Implementing the iterative stack version demonstrates deeper understanding of DFS mechanics and control over recursion.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive Depth-First Search (DFS) | O(n) | O(h) | Best general solution. Clean and intuitive for tree traversal problems. |
| Iterative DFS with Stack | O(n) | O(h) | Useful when avoiding recursion or when stack-based traversal control is preferred. |