What is output limit exceeded?


  • 1
    C
    class Solution {
    public:
    TreeNode *sortedListToBST(ListNode *head) {
    	ListNode* current;
    	current = head;
    	if (NULL != head){
    		while (NULL != current->next) current = current->next;
    	}
    	
    	return helperFunction (head, current);
    }
    
    TreeNode* helperFunction(ListNode* start, ListNode* end){
    	ListNode* mid = NULL;
    	TreeNode* root = NULL;
    	
    	if (NULL == start){
    		return NULL;
    	}
    	
    	if (start == end){
    		return new TreeNode(start->val);	
    	}
    	
    	if (start->next == end){
    		root = new TreeNode(start->val);
    		
    		root->right = new TreeNode(end->val);
    		return root;
    	}
    
    	
    	mid = findMid(start, end);
    	
    	root = new TreeNode(mid->val);
    	root->left = helperFunction(start, mid);
    	
    	root->right = helperFunction(mid->next, end);
    
    	return root;
    }
    
    ListNode* findMid(ListNode* head, ListNode* end){
    	ListNode* slow;
    	ListNode* fast;
    	
    	slow = head;
    	if (NULL != slow){
    		fast = slow->next;
    		
    		if (end != fast){
    			fast = fast->next;
    		}
    	}
    	
    	while (end != fast){
    		slow = slow->next;
    		
    		fast = fast->next;
    		
    		if (end != fast){
    			fast = fast->next;
    		}
    	}
    	
    	return slow;
    }
    

    };


Log in to reply
 

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