(TreeNode root, int targetSum, int currSum)
| 16 | class Solution { |
| 17 | //This would be easily solved by DFS and then comparing the values |
| 18 | public boolean dfs(TreeNode root, int targetSum, int currSum){ |
| 19 | if(root == null) return false; |
| 20 | |
| 21 | currSum += root.val; |
| 22 | if(root.left == null && root.right == null){ |
| 23 | return (currSum == targetSum); |
| 24 | } |
| 25 | return dfs(root.left, targetSum, currSum) || dfs(root.right, targetSum, currSum); |
| 26 | } |
| 27 | public boolean hasPathSum(TreeNode root, int targetSum) { |
| 28 | return dfs(root, targetSum, 0); |
| 29 | } |