0ms c based solution


  • 0
    int row = 0;
    int count = 0;
    struct TreeNode* recurse(struct TreeNode* root, int** result)
    {
        if(!root)
        return NULL;
    
        struct TreeNode* left = recurse(root->left,result);
        struct TreeNode* right = recurse(root->right,result);
    
        if(!root->right && !root->left)
        {
           if(result[row])
              result[row] = realloc(result[row], (count+1)*sizeof(int));
           else
              result[row] = malloc((count+1)*sizeof(int));
           result[row][count] = root->val;
           count++;
           return root;
    }
    
    if(right||left)
    {
       root->left = root->left==right?NULL:root->left;
       root->right = root->right==right?NULL:root->right;
       root->left = root->left==left?NULL:root->left;
       root->right=root->right==left?NULL:root->right;
    }
    
    return NULL;
    }
    
    int** findLeaves(struct TreeNode* root, int** columnSizes, int* returnSize) {
        int** result = malloc(100*sizeof(int*));
        result[0]=NULL;
        *columnSizes = malloc(sizeof(int));
        struct TreeNode* dummy = malloc(sizeof(struct TreeNode));
    
        dummy->right = root;
        dummy->left = NULL;
        while(dummy->right)
        {
            recurse(dummy,result);
            *columnSizes = realloc(*columnSizes,(row+1)*sizeof(int));
            columnSizes[0][row++]=count;
            result[row]=NULL;
            count = 0;
           
        }
    
        *returnSize = row;
        row = 0;
        count = 0;
        return result;    
    }

Log in to reply
 

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