LeetCode solutions

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

Read the problem on LeetCode View on GitHub

C++

Accepted on LeetCode — runtime 0ms, memory 17.2MB, accepted 2025-12-27.

cpp
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);
    }
};

Source