题解 | #二叉树中和为某一值的路径(二)#
二叉树中和为某一值的路径(二)
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;
}