1. 程式人生 > >leetcode 112:路徑總和

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