Easy to understand Java solution (0 ms)


  • 0
    T
    public class Solution {
        int ans = 0;
        int sum=0;
        public int sumNumbers(TreeNode root) {
            SumUp(root);
            return ans;
        }
        void SumUp(TreeNode t){
            if(t==null)return;
            sum = sum*10+t.val;
            if(t.left==null && t.right==null){
                ans+=sum;
                sum = sum/10;
                return;
            }
            else{
            SumUp(t.left);
            SumUp(t.right);
            }
            sum=sum/10;
        }
    }
    

Log in to reply
 

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