Sum of Root To Leaf Binary Numbers
Easy
Topics
Every node has value 0 or 1. Each root-to-leaf path represents a binary number (most significant bit first). Return the sum, in base 10, of all root-to-leaf binary numbers.
Example 1
acc * 2 + node.val, which is just a shift and an or. The 4 root-to-leaf path(s) total 22.null marks a missing child.Input: root = [1,0,1,0,1,0,1] Output: 22
Example 2
Input: root = [1,1] Output: 3
Constraints
- 1 <= number of nodes <= 1000
- Node.val is 0 or 1
Editorial — intuition, approach, complexity and pitfalls
Intuition
Each root-to-leaf path spells a binary number, most significant bit first. Building the path as a list and converting it at each leaf works, but there is no need: the value can be carried down and extended on the way.
Appending a bit to a binary number is value * 2 + bit — shift left, OR in the new digit. So each node passes its parent's accumulated value, doubled and incremented by its own bit, to its children. At a leaf, that accumulator is the number, so return it; internal nodes return the sum of their subtrees.
This is Horner's method again, the same shift-and-add used for base conversion, applied along a tree path instead of across a string.
Approach
- DFS carrying
current, initially 0. - At each node,
current = current * 2 + node.val. - At a leaf, return
current; otherwise return the sum of both child calls.
Dry Run
tree 1
/ \
0 1
/ \
0 1
path 1->0->0 : 1, then 1*2+0=2, then 2*2+0=4 -> 4
path 1->0->1 : 1, then 2, then 2*2+1=5 -> 5
path 1->1 : 1, then 1*2+1=3 -> 3
answer 4 + 5 + 3 = 12
check: 100 = 4, 101 = 5, 11 = 3 ✓Complexity
- Time — O(n): each node visited once, constant work per node.
- Space — O(h): the recursion stack. Carrying the value as an integer rather than building a list per path is what keeps this O(h) instead of O(h) per path with allocation.
Common Pitfalls
- Treating a node with exactly one child as a leaf. A leaf has both children null; a one-child node is internal and its path continues.
- Returning 0 at a null node and also counting it as a path. Null contributes nothing, which the base case handles — but the leaf test must come before recursing, not instead of it.
- Building a string per path and parsing it. Correct, and it allocates per leaf for no gain.
- Using
current + node.valinstead ofcurrent * 2 + node.val, which sums the bits rather than composing a binary number.
Full Solution — all languages
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def sumRootToLeaf(self, root):
def dfs(node, cur):
if not node: return 0
cur = cur * 2 + node.val
if not node.left and not node.right: return cur
return dfs(node.left, cur) + dfs(node.right, cur)
return dfs(root, 0)import java.util.*;
class TreeNode {
int val;
TreeNode left, right;
TreeNode(int v) {
val = v;
}
}
class Solution {
public int sumRootToLeaf(TreeNode root) {
return dfs(root, 0);
}
private int dfs(TreeNode node, int cur) {
if (node == null) return 0;
cur = cur * 2 + node.val;
if (node.left == null && node.right == null) return cur;
return dfs(node.left, cur) + dfs(node.right, cur);
}
}#include <bits/stdc++.h>
using namespace std;
struct TreeNode {
int val;
TreeNode *left, *right;
TreeNode(int v): val(v), left(nullptr), right(nullptr) {
}
};
class Solution {
public: int sumRootToLeaf(TreeNode* root) {
function<int(TreeNode*,int)> dfs = [&](TreeNode* node, int cur) -> int {
if (!node) return 0;
cur = cur * 2 + node->val;
if (!node->left && !node->right) return cur;
return dfs(node->left, cur) + dfs(node->right, cur);
};
return dfs(root, 0);
}
};/**
* Tree node shape: { val, left, right }
* @param {TreeNode} root
* @return {number}
*/ var sumRootToLeaf = function(root) {
const dfs = (node, cur) => {
if (!node) return 0;
cur = cur * 2 + node.val;
if (!node.left && !node.right) return cur;
return dfs(node.left, cur) + dfs(node.right, cur);
};
return dfs(root, 0);
};