Java Recursive with addition


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

Log in to reply
 

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