class Solution {public boolean hasPathSum(TreeNode root, int targetSum) {// 这样递归才有终止条件if (root == null) {return false;// 题目的诉求:根节点到叶子节点 的路径,所以要走到其没有左右子树} else if (root.left == null && root.right == null) {// 走到叶子节点这判断该条路是否为正确路径return targetSum == root.val;} else {int curVal = targetSum - root.val;return hasPathSum(root.left, curVal) || hasPathSum(root.right, curVal);}}}
