Skip to content

Latest commit

 

History

History
17 lines (16 loc) · 373 Bytes

File metadata and controls

17 lines (16 loc) · 373 Bytes
  • 递归往子节点查找即可
class Solution {
 public:
  bool hasPathSum(TreeNode* root, int targetSum) {
    if (!root) {
      return false;
    }
    if (!root->left && !root->right) {
      return root->val == targetSum;
    }
    return hasPathSum(root->left, targetSum - root->val) ||
           hasPathSum(root->right, targetSum - root->val);
  }
};