【leetcode】113. 路径总和 II

 

void recursion(struct TreeNode* root,int sum,int* returnSize,int* returnColumnSizes,int**arr,int* temp,int cnt,int* total){
    if(!root) return;    
    *(total)+=root->val;
    temp[cnt]=root->val;
    if(!root->left && !root->right && *total == sum){
        arr[(*returnSize)]=(int*)calloc(1000,sizeof(int));
        memcpy(arr[(*returnSize)],temp,(cnt+1)*sizeof(int));
        returnColumnSizes[(*returnSize)++]=cnt+1;
    }
    recursion(root->left,sum,returnSize,returnColumnSizes,arr,temp,cnt+1,total);
    recursion(root->right,sum,returnSize,returnColumnSizes,arr,temp,cnt+1,total);
    *(total)-=root->val;
}
int** pathSum(struct TreeNode* root, int sum, int* returnSize, int** returnColumnSizes){
    int** arr=(int**)calloc(1000,sizeof(int*));
    *returnColumnSizes=(int*)calloc(1000,sizeof(int));
    *returnSize=0;
    int total=0;
    int temp[1000];
    recursion(root,sum,returnSize,*returnColumnSizes,arr,temp,0,&total);
    return arr;
}

 

posted @ 2020-12-17 18:42  温暖了寂寞  阅读(55)  评论(0编辑  收藏  举报