题解 | #二叉树中和为某一值的路径(二)#

二叉树中和为某一值的路径(二)

http://www.nowcoder.com/practice/b736e784e3e34731af99065031301bca

本着能不用全局变量就不用全局变量的原则

void dfs(struct TreeNode* root,int target,int** returnColumnSizes,int* returnSize,int count,int* path,int** res){
    if(root==NULL){
        return;
    }
    path[count++]=root->val;
    target-=root->val;
    if(root->right==NULL&&root->left==NULL&&target==0){
        int* temp=(int*)malloc(sizeof(int)*count);
        for(int i=0;i<count;i++){
            temp[i]=path[i];
        }
        res[(*returnSize)]=temp;
        (*returnColumnSizes)[(*returnSize)++]=count;
    }
    dfs(root->left,target,returnColumnSizes,returnSize,count,path,res);
    dfs(root->right,target,returnColumnSizes,returnSize,count,path,res);
    count--;
}


int** FindPath(struct TreeNode* root, int target, int* returnSize, int** returnColumnSizes){
    int** res=(int**)malloc(sizeof(int*)*5000);
    *returnColumnSizes=(int*)malloc(sizeof(int)*5000);
    int* path=(int*)malloc(sizeof(int)*5000);
    int count=0;
    *returnSize=0;
    dfs(root,target,returnColumnSizes,returnSize,count,path,res);
    return res;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-11 17:10
什么素质,我请问呢,要掉小珍珠了。。。又憋屈又生气
苍蓝星上艾露:给它们能的,一群dinner牛马挥刀向更弱者罢了。我写的开源求职AI co-pilot工具,优化你的简历,找到你匹配的岗位,定制你的简历,并让你做好面试准备https://github.com/weicanie/prisma-ai
点赞 评论 收藏
分享
06-04 09:27
门头沟学院 Java
点赞 评论 收藏
分享
07-05 16:23
门头沟学院 Java
mengnankk:我投了300,约了5 6个面试。感觉项目写的太多了。一个项目就写五六个亮点,不是把整个项目的功能描述下。其他的没啥,简历看起来有点长
点赞 评论 收藏
分享
07-11 13:16
湖南工学院 Java
坚定的芭乐反对画饼_...:谁也不知道,毕竟现在的互联网和十年前已经完全不同了,谁都无法预测未来
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务