When i submit my code it shows internal error with no further details why is it so?


  • 0
    K

    /**

    • Definition for singly-linked list.
    • struct ListNode {
    • int val;
      
    • ListNode *next;
      
    • ListNode(int x) : val(x), next(NULL) {}
      
    • };
      /
      /
      *
    • Definition for a binary tree node.
    • struct TreeNode {
    • int val;
      
    • TreeNode *left;
      
    • TreeNode *right;
      
    • TreeNode(int x) : val(x), left(NULL), right(NULL) {}
      
    • };
      /
      class Solution {
      public:
      TreeNode
      sortedListToBST(ListNode* head)
      {
      if(head == NULL)
      {
      return NULL;
      }
      int len =0 ;
      ListNode* temp = head,mid_node=NULL;
      while(temp != NULL)
      {
      len++;
      temp = temp->next;
      }
      temp = head;
      int mid = len/2,cmid=mid,copy=mid;
      if(mid == 0)
      {
      TreeNode
      root = new TreeNode(head->val);
      return root;
      }
      while(mid > 0)
      {
      mid_node = temp;
      temp = temp->next;
      mid--;
      }
      TreeNode* root = new TreeNode(mid_node->val);
      TreeNode* temp1=new TreeNode(head->val),*temp2=new TreeNode(head->val);
      while(cmid > 1)
      {
      if(cmid == copy)
      {
      temp1 = new TreeNode(head->val);
      }
      else
      {
      temp2 = new TreeNode(head->val);
      temp2->left = temp1;
      temp1 = temp2;
      }
      head=head->next;
      cmid--;
      }
      if(cmid == copy)
      {
      root->left = NULL;
      }
      else
      {
      root->left = temp2;
      }
      head = head->next;
      while(head != NULL)
      {
      if(cmid == 1)
      {
      temp1 = new TreeNode(head->val);
      }
      else
      {
      temp2 = new TreeNode(head->val);
      temp2->right = temp1;
      temp1 = temp2;
      }
      head=head->next;
      cmid--;
      }
      if(cmid != 0)
      {
      root->right = temp2;
      }
      else
      {
      root->right = temp1;
      }
      return root;
      }
      };

Log in to reply
 

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