Simple java solution accepted


  • 4
    P
    public class Solution {
        public int sumNumbers(TreeNode root) {
            return sumNumbersUtil(root, 0);
        }
        
        public int sumNumbersUtil(TreeNode root, int path) {
            if (root == null) {
                return 0;
            }
            
            path = path*10 + root.val;
    
            if (root.left == null && root.right == null) {
                return path;
            }
    
            return sumNumbersUtil(root.left, path) + sumNumbersUtil(root.right, path);
        }
    }

Log in to reply
 

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