Path Sum
EasyTreeDepth-First SearchBreadth-First SearchBinary Tree
Solution
import { TreeNode } from '@algorithm/lib';
export function hasPathSum(root: TreeNode | null, targetSum: number): boolean {
if (root === null) {
return false;
}
if (root.left === null && root.right === null && root.val === targetSum) {
return true;
}
return (
hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val)
);
}