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);
}

猜你喜欢

转载自blog.csdn.net/u013263891/article/details/82903655