Java solution using recursive,1ms


  • 0
    public boolean hasPathSum(TreeNode root, int sum) {
        if(root == null) return false;
        
        if(root.left == null && root.right == null && root.val == sum)
            return true;
        
        boolean ans1=false , ans2=false; 
        if(!(root.left == null)){
            root.left.val += root.val;
            ans1 = hasPathSum(root.left,sum);
        }
        
        if(!(root.right == null)){
            root.right.val += root.val;
            ans2 = hasPathSum(root.right,sum);
        }
        
        return ans1||ans2;
    
    }

Log in to reply
 

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