Java recursive 6 line


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

Log in to reply
 

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