Simple,easy C program ( 0ms


  • 0
    I

    void dfs(char ra, struct TreeNode root, int returnSize, char* path, int lp){

    int i,r;
    char temp[10];
    
    //lp = last position
    path[lp] = root->val;
    lp++;
    
    if(root->left == NULL && root->right == NULL)
    {
        (*returnSize)++;
        r = (*returnSize)-1;
        
        ra[r] = malloc(sizeof(char) *10 *lp);
        ra[r][0] = '\0';
        
        for(i = 0; i< lp-1;i++){
            sprintf(ra[r],"%s%d->",ra[r], path[i]);
        }
        sprintf(ra[r],"%s%d",ra[r], path[i]);
        
        return;
    }
    
    if(root->left != NULL){
        dfs(ra,root->left,returnSize,path,lp);
    }    
    if(root->right != NULL){
        dfs(ra,root->right,returnSize,path,lp);
    }
    return;
    

    }

    char** binaryTreePaths(struct TreeNode* root, int* returnSize) {

    char** ra; //returnArray
    int* path;
    
    int size = 200;
    
    if(root == NULL)
        return NULL;
        
    path = malloc(sizeof(int ) * size);
    ra = malloc(sizeof(char *) * size);
    
    (*returnSize) = 0;
    dfs(ra,root,returnSize,path,0);
    
    return ra; 
    

    }


  • 0
    S
    void dfs(char **ra, struct TreeNode* root, int* returnSize, char* path, int lp)
    

    should path be a int *?


  • 0
    I

    Yes you are right.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.