Description
Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.
A leaf is a node with no children.
Examples
Example 1:
  >Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
>Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
 Output: true
 Explanation: The root-to-leaf path with the target sum is shown.
Example 2:
 
Example 3:
Constraints:
思路
递归
代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null)
            return false;
        if (root.left == null && root.right == null)
            return root.val == targetSum;
        
        boolean canLeft = false, canRight = false;
        if (root.left != null)
            canLeft = hasPathSum(root.left, targetSum - root.val);
        if (canLeft)
            return true;
        if (root.right != null)
            canRight = hasPathSum(root.right, targetSum - root.val);
        
        return canRight;
    }
}









