leetcode 112:路径总和

感觉二叉树用递归基本都能做出来,这个也比较简单

bool hasPathSum(TreeNode* root,int sum){
    if(root==NULL)
        return false;
    TreeNode*l=root->left;
    TreeNode*r=root->right;
    if(l==NULL&&r==NULL&&root->val==sum)
        return true;
    else
        return hasPathSum(l,sum-root->val)||hasPathSum(r,sum-root->val);
}

 

相关文章:

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