首页 > 其他 > 详细

路径总和--leetcode112

时间:2020-07-07 09:52:07      阅读:45      评论:0      收藏:0      [点我收藏+]
技术分享图片

 

 方法1:递归

 
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */


bool hasPathSum(struct TreeNode* root, int sum){
    if(root==NULL)
    {
        return false;
    }
    if(root->left==NULL&&root->right==NULL)
    {
        return sum==root->val;
    }
    return hasPathSum(root->left,sum-root->val)||hasPathSum(root->right,sum-root->val);
}
技术分享图片

路径总和--leetcode112

原文:https://www.cnblogs.com/sbb-first-blog/p/13258872.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!