1. 题目

Leetcode#112. 路径总和,C++实现

2. 方法一

2.1. 代码

class Solution {
public:
    bool hasPathSum(TreeNode* root, int sum) {
        if(root==NULL) return false;
        if(root->left==NULL&&root->right==NULL&&root->val==sum) return true;
        return hasPathSum(root->left,sum-root->val)||hasPathSum(root->right,sum-root->val);
        
    }
};

2.2. 结果

Leetcode#112. 路径总和,C++实现

相关文章:

  • 2022-02-24
  • 2021-07-17
  • 2021-08-16
  • 2022-12-23
  • 2021-07-26
  • 2021-08-01
  • 2022-12-23
猜你喜欢
  • 2021-05-12
  • 2021-06-07
  • 2021-12-18
  • 2021-12-31
  • 2022-12-23
  • 2022-02-21
  • 2022-01-15
相关资源
相似解决方案