Solution 1: accepted 1ms
DFS.
|
|
Simplified123456public boolean hasPathSum(TreeNode root, int sum) { if (root == null) return false; else if (sum - root.val == 0 && root.left == null && root.right == null) return true; return (hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val));}