Untitled
unknown
plain_text
a year ago
390 B
7
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