# Memory Limit Exceeded

• What's the memory limit?

Do I need to delete all ListNode?

Thats's my code:

``````TreeNode *sortedListToBST(ListNode *head) {
vector<TreeNode *> nodes;
return NULL;
}
TreeNode * temp_tree = new TreeNode(head->val);
nodes.push_back(temp_tree);
ListNode * victim = head;
delete victim;
temp_tree = new TreeNode(head->val);
nodes.push_back(temp_tree);
}

return make_tree(nodes, 0, nodes.size()-1);
}

TreeNode * make_tree(vector<TreeNode *> nodes, int head, int tail){
int root_index = (head+tail)/2;
}else if(tail == head+1){
}else{
nodes[root_index]->right = make_tree(nodes, root_index+1, tail-1);
nodes[root_index]->left = make_tree(nodes, head, root_index-1);
return nodes[root_index];
}
}``````

• No, you do not need delete listnode. can you explain your algorithm? I do not know why

``````nodes[root_index]->right = make_tree(nodes, root_index+1, tail-1);
``````

why it is " tail -1 ", not " tail "?

• Sorry, that's one of my mistake, and it should be tail.
The median of the list is the root of the BST, and recursively do the same process to the left part and the right part.
This ALG is accepted when the input is a sorted array in another question and has MLE in this question, so I don't know why.

• I find your bug. TreeNode * make_tree(vector<TreeNode *> nodes, int head, int tail) should be TreeNode * make_tree(vector<TreeNode *> &nodes, int head, int tail). when this function is called, nodes will be copied, which waste a lot memory. So if it is reference, no copy is needed.

• It works! Thank you.

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