Any idea why this produces a runtime error?

  • 0
    void insert(TreeNode* root, int value) {
    	TreeNode** pos = &root;
    	while (*pos) {
    		if (value < (*pos)->val) {
    			pos = &(*pos)->left;
    		} else {
    			pos = &(*pos)->right;
    	*pos = new TreeNode(value);
    TreeNode* sortedListToBST(ListNode *head) {
        if (!head) return NULL;
    	ListNode* current = head;
    	TreeNode* root = new TreeNode(current->val);
    	current = current->next;
    	while(current) {
    		insert(root, current->val);
    		current = current->next;
    	return root;

  • 0

    You should use NULL to determine if there is a node. Besides, you are trying to generate a tree with the maximum depth instead of a height balanced BST.

Log in to reply

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