# Definition for a binary tree node.
# 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: Optional[TreeNode]) -> int:
def dfs(node, curr):
if not node:
return 0
curr = (curr << 1) | node.val
if not node.left and not node.right:
return curr
return dfs(node.left, curr) + dfs(node.right, curr)
return dfs(root, 0)
// Definition for a binary tree node.
// struct TreeNode {
// int val;
// TreeNode *left;
// TreeNode *right;
// TreeNode() : val(0), left(nullptr), right(nullptr) {}
// TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
// TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
// };
class Solution {
public:
int sumRootToLeaf(TreeNode* root) {
return dfs(root, 0);
}
private:
int dfs(TreeNode* node, int curr) {
if (!node) return 0;
curr = (curr << 1) | node->val;
if (!node->left && !node->right) return curr;
return dfs(node->left, curr) + dfs(node->right, curr);
}
};
// 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 sumRootToLeaf(TreeNode root) {
return dfs(root, 0);
}
private int dfs(TreeNode node, int curr) {
if (node == null) return 0;
curr = (curr << 1) | node.val;
if (node.left == null && node.right == null) return curr;
return dfs(node.left, curr) + dfs(node.right, curr);
}
}
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var sumRootToLeaf = function(root) {
function dfs(node, curr) {
if (!node) return 0;
curr = (curr << 1) | node.val;
if (!node.left && !node.right) return curr;
return dfs(node.left, curr) + dfs(node.right, curr);
}
return dfs(root, 0);
};
Given the root of a binary tree where each node contains a value of 0
or 1
, each root-to-leaf path can be interpreted as a binary number. The task is to compute the sum of all root-to-leaf binary numbers represented by the tree.
Constraints:
[1, 1000]
.0
or 1
.To solve this problem, we need to traverse all paths from the root to every leaf node in the binary tree. For each path, we interpret the sequence of node values as a binary number. The main challenge is to efficiently accumulate the binary value as we traverse and sum all such numbers.
A brute-force approach would involve:
We use a depth-first search (DFS) traversal to visit every root-to-leaf path. At each node, we maintain the current binary number formed by the path from the root to the current node.
This approach is efficient because:
Consider the following binary tree:
1 / \ 0 1 / \ / \ 0 1 0 1
The root-to-leaf paths are:
1 → 0 → 0
(binary 100) = 41 → 0 → 1
(binary 101) = 51 → 1 → 0
(binary 110) = 61 → 1 → 1
(binary 111) = 74 + 5 + 6 + 7 = 22
.
Step-by-step, as we traverse:
Brute-force approach:
The "Sum of Root To Leaf Binary Numbers" problem is elegantly solved using a recursive DFS traversal. By accumulating the binary number as we traverse and using bit manipulation, we avoid unnecessary storage and redundant computation. This approach is both intuitive and efficient, leveraging the structure of the tree and properties of binary numbers for a clean solution.