Gives RTE but runs locally


  • 0
    A

    Hi,

    Following is my solution in C, this solution gives run time error for input [1] but locally when I run it works perfectly, I am using VC++ 2010, Can anybody please point out what I am missing.

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    
    #define MAX 100
    
    char **result = NULL;
    char tempResult[MAX][MAX];
    char path[MAX];
    int numPath = 0;
    
    /*struct TreeNode {
    	int val;
    	struct TreeNode *left;
    	struct TreeNode *right;
    };*/
    
    /**
    * Definition for a binary tree node.
    * struct TreeNode {
    *     int val;
    *     struct TreeNode *left;
    *     struct TreeNode *right;
    * };
    */
    /**
    * Return an array of size *returnSize.
    * Note: The returned array must be malloced, assume caller calls free().
    */
    
    void display(char *a, int n)
    {
    	int i = 0;
    	while(i <= n)
    	{
    		printf("%c ",a[i]);
    		i++;
    	}
    	printf("\n");
    }
    
    void store(int returnSize)
    {
    	int i = 0;
    	result = (char**)malloc(sizeof(char*)*returnSize);
    	if(!result)
    	{
    		exit(1);
    	}
    	while(i < returnSize)
    	{
    		result[i] = (char*)malloc(strlen(tempResult[i])+1);
    		if(!result[i])
    		{
    			exit(1);
    		}
    		strcpy(result[i], tempResult[i]);
    		++i;
    	}
    }
    void helper(struct TreeNode* root, int index)
    {
    	if(!root)
    	{
    		return;
    	}
    	if(root->left == NULL &&
    		root->right == NULL
    		)
    	{
    		path[index] = root->val+'0';
    		//display(path, index);
    		//store(path, index+1);
    		tempResult[numPath][index+1] = '\0';
    		strncpy(tempResult[numPath], path, index+1);
    		numPath++;
    		return;
    	}
    	path[index] = root->val+'0';
    	if(root->left)
    	{
    		helper(root->left, index+1);
    	}
    	if(root->right)
    	{
    		helper(root->right, index+1);
    	}
    }
    
    char** binaryTreePaths(struct TreeNode* root, int* returnSize) {
    	int i;
    	helper(root, 0);
    	*returnSize = numPath;
    	if(*returnSize > 0)
    	{
    		store(*returnSize);
    	}
    	numPath = 0;
    
    	/*for(i=0; i<*returnSize; ++i)
    		display(result[i], strlen(result[i]));*/
    }
    

    Regards,
    Arka


  • 0
    A

    Tested with gcc and works there as well.....


  • 0
    A

    jianchao.li.fighter Can you please help me out here ?


Log in to reply
 

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