My Java solution using Recursive


  • 8
    V
    public boolean hasPathSum(TreeNode root, int sum) {
        if (root == null) {
            return false;
        } else {
            if (root.left == null && root.right == null) {
                return sum == root.val;
            } else {
                int subSum = sum - root.val;
                return hasPathSum(root.left, subSum) || hasPathSum(root.right, subSum);
            }
        }
    }

Log in to reply
 

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