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

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

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;
}
全部评论

相关推荐

2025-12-18 18:23
深圳大学 前端工程师
程序员花海:实习和校招简历正确格式应该是教育背景+实习+项目经历+个人评价 其中项目经历注意要体现业务 实习经历里面的业务更是要自圆其说 简历模板尽可能保持干净整洁 不要太花哨的
点赞 评论 收藏
分享
LastWh1spe...:ssob真有些人和那个没睡醒一样
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

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