Neat 0ms Java solution


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

Log in to reply
 

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