1079. Sum of Root To Leaf Binary Numbers
My accepted C++ solution to LeetCode problem 1079, Sum of Root To Leaf Binary Numbers, running in 0ms.
- Difficulty: Easy
- C++
- Runtime 0ms
- Memory 17.2MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
C++
Accepted on LeetCode — runtime 0ms, memory 17.2MB, accepted 2025-12-27.
class Solution {
public:
int sumRootToLeaf(TreeNode* root, int val = 0) {
if (!root) return 0;
val = val * 2 + root->val;
if (!root->left && !root->right) return val;
return sumRootToLeaf(root->left, val) + sumRootToLeaf(root->right, val);
}
};