c# solution


  • 0
    M

    public class Solution {
    private int sum = 0;

        public int SumNumbers(TreeNode root)
        {            
            if (root == null) return 0;
    
            SumNumbersRec(root, 0);
    
            return sum;
        }
    
        private void SumNumbersRec(TreeNode node, int num)
        {
            int number = num * 10 + node.val;
            
            if (node.left != null)
            {
                SumNumbersRec(node.left, number);
            }
    
            if (node.right != null)
            {
                SumNumbersRec(node.right, number);
            }
    
            if (node.left == null && node.right == null)
            {
                sum += number;
            }
        }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.