In this Leetcode Sum Root to Leaf Numbers problem solution we have given the root of a binary tree containing digits from 0 to 9 only. Each root-to-leaf path in the tree represents a number.

For example, the root-to-leaf path 1 -> 2 -> 3 represents the number 123. Return the total sum of all root-to-leaf numbers. Test cases are generated so that the answer will fit in a 32-bit integer. A leaf node is a node with no children.

Leetcode Sum Root to Leaf Numbers problem solution


Problem solution in Python.

class Solution:
    def sumNumbers(self, root: TreeNode) -> int:
        s=0
        ps=""
        if root==None:
            return
        def dfs(root,ps,t):
            op=0
            
            if root==None:
                return
            if root.left==None and root.right==None:
                
                ps=ps+str(root.val)
                ps=ps[::-1]
                i=1
                op=0
                for i in range(len(ps)):
                    op=op+int(ps[i])*(10**i)
                    i=i+1
                
                t=t+op
            if root.left:
                t=dfs(root.left,ps+str(root.val),t)
            if root.right:
                t=dfs(root.right,ps+str(root.val),t)
            return t
        a=dfs(root,ps,s)
        return a



Problem solution in Java.

class Solution {
    private int res;
    
    public int sumNumbers(TreeNode root) {
        help(root, 0);
        return res;
    }
    
    private void help(TreeNode node, int num) {
        if (node == null) {
            return;
        }
        num = num * 10 + node.val;
        if (node.left == null && node.right == null) {
            res += num;
        }
        help(node.left, num);
        help(node.right, num);
    }
}


Problem solution in C++.

long totalvalue;
    long getvalue(string s)
    {
        long r=0;
        for(int i=s.size()-1;i>=0;i--)
            r+=(s[i]-'0')*pow(10,s.size()-i-1);
        return r;
    }
    void PreoderTraversal(TreeNode* node,string curval)
    {
        if(node!=nullptr)
        {
            if(node->left!=nullptr)
                PreoderTraversal(node->left,curval+to_string(node->val));
            if(node->right!=nullptr)
                PreoderTraversal(node->right,curval+to_string(node->val));
            if(node->left==nullptr and node->right==nullptr)
               totalvalue+=getvalue(curval+to_string(node->val));
            
        }
    }
    int sumNumbers(TreeNode* root) {
        totalvalue=0;
        PreoderTraversal(root,"");
        return totalvalue;
    }


Problem solution in C.

int global_sum = 0;

void sum_root_to_leaves(struct TreeNode * root, int sum)
{
    if(root == NULL)
        return;
    
    int new_sum = (sum*10) + root->val;
    
    if((root->left == NULL) && (root->right == NULL))
    {
        global_sum += new_sum;
        return;
    }
        
    sum_root_to_leaves(root->left, new_sum);
    sum_root_to_leaves(root->right, new_sum);
   
}


int sumNumbers(struct TreeNode* root)
{
    global_sum = 0;
    int sum = 0;
    sum_root_to_leaves(root, sum);
    
    return global_sum;
    
}