Simple and clean java solution based on Pre-order traversal.


  • 4
    J
     public int sumNumbers(TreeNode root) {
        return sumNumbers(root, 0);
     }
    
    private int sumNumbers(TreeNode root, int sum) {
    	if (root == null) return 0;
    	sum = sum * 10 + root.val;
    	if (root.left == null && root.right == null) {
    		return sum;
    	}
    	return sumNumbers(root.left, sum) + sumNumbers(root.right, sum);
    }

Log in to reply
 

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