Java straight forward AC solution


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

    Pretty straight forward solution without any polish. Hope it help you get the basic idea of solving this problem.


Log in to reply
 

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