Untitled

Anonymous
plain_text
01/02/2025 4:39 PM
520 B
18
Indexable
public class PathSum {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) return false;

        targetSum -= root.val;
        if (root.left == null && root.right == null) { // Check if it's a leaf
            return targetSum == 0;
        }

        return hasPathSum(root.left, targetSum) || hasPathSum(root.right, targetSum);
    }
}
Editor is loading...
Leave a Comment