[ Output Limit Exceeded ] what's wrong with my code?


  • 0
    F

    This is my code and get "Output Limit Exceeded" error. who can tell me what this error means and any problem with my code? Thanks!

    class Solution {
    
    public:
    	vector<TreeNode *> generateTrees(int n) {
    	    
    		vector<vector<vector<TreeNode *> > > mid(n, vector<vector<TreeNode *> >(n));
    		return generateTrees(mid, 1, n);
    	}
    
    	vector<TreeNode *> generateTrees(vector<vector<vector<TreeNode *> > >& mid, int start, int end)    {
    		
    		vector<TreeNode *> vec;
    		if (start > end) {
    		    vec.push_back(NULL);
    			return vec;
    		}
    		if (!mid[start-1][end-1].empty()) {
    			return mid[start-1][end-1];
    		}
    		
    		for (int i = start; i <= end; i++) {
    
    			vector<TreeNode *> leftList = generateTrees(mid, 1, i - 1);
    			vector<TreeNode *> rightList = generateTrees(mid, i + 1, end);
    			for (int ll = 0; ll < leftList.size(); ll++) {
    				for (int rr = 0; rr < rightList.size(); rr++) {
    					TreeNode *root = new TreeNode(i);
    					root->left = leftList[ll];
    					root->right = rightList[rr];
    					vec.push_back(root);
    				}
    			}
    			
    		}
    		mid[start-1][end-1] = vec;
    		return vec;
    	}
    };

Log in to reply
 

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