/* The main function that constructs balanced BST and returns root of it. head_ref --> Pointer to pointer to head node of Doubly linked list n --> No. of nodes in the Doubly Linked List */ struct Node* sortedListToBSTRecur(struct Node **head_ref, int n) { /* Base Case */ if (n <= 0) return NULL; /* Recursively construct the left subtree */ struct Node *left = sortedListToBSTRecur(head_ref, n/2); /* head_ref now refers to middle node, make middle node as root of BST*/ struct Node *root = *head_ref; // Set pointer to left subtree root->prev = left; /* Change head pointer of Linked List for parent recursive calls */ *head_ref = (*head_ref)->next; /* Recursively construct the right subtree and link it with root The number of nodes in right subtree is total nodes - nodes in left subtree - 1 (for root) */ root->next = sortedListToBSTRecur(head_ref, n-n/2-1); return root; }
TreeNode *sortedListToBSTRecur(int start, int end, ListNode *head) { if (end<start) { return NULL; } int middle = (start + end + 1)/2; ListNode *list_probe = head; for(int i = 0; i != middle; ++i) { list_probe = list_probe->next; } TreeNode *middle_node = new TreeNode(0); middle_node->val = list_probe->val; middle_node->left = sortedListToBSTRecur(start, middle-1, head); middle_node->right = sortedListToBSTRecur(middle+1, end, head); return middle_node; }
/* This function counts the number of nodes in Linked List and then calls sortedListToBSTRecur() to construct BST */ struct Node* sortedListToBST(struct Node *head) { /*Count the number of nodes in Linked List */ int n = countNodes(head); /* Construct BST */ return sortedListToBSTRecur(&head, n); }
TreeNode sortedListToBST(Node head) { /*Count the number of nodes in Linked List */ int n = count(head); /* Construct BST */ return sortedListToBSTRecur(&head, n); }
/* This function counts the number of nodes in Linked List and then calls sortedListToBSTRecur() to construct BST */ struct TNode* sortedListToBST(struct LNode *head) { /*Count the number of nodes in Linked List */ int n = countLNodes(head); /* Construct BST */ return sortedListToBSTRecur(&head, n); }
/* The main function that constructs balanced BST and returns root of it. head_ref --> Pointer to pointer to head node of linked list n --> No. of nodes in Linked List */ TreeNode sortedListToBSTRecur(Node *head_ref, int n) { /* Base Case */ if (n <= 0) return NULL; /* Recursively construct the left subtree */ TreeNode left = sortedListToBSTRecur(head_ref, (int)(n / 2)); /* Allocate memory for root, and link the above constructed left subtree with root */ TreeNode root = createTreeNode((*head_ref)->data); root->left = left; /* Change head pointer of Linked List for parent recursive calls */ *head_ref = (*head_ref)->next; /* Recursively construct the right subtree and link it with root The number of nodes in right subtree is total nodes - nodes in left subtree - 1 (for root) which is n-n/2-1*/ root->right = sortedListToBSTRecur(head_ref, n - n / 2 - 1); return root; }
TreeNode *sortedListToBST(ListNode *head) { if (head == NULL) { return NULL; } ListNode *list_probe = head; int length = 0; while(list_probe != NULL) { list_probe = list_probe->next; length++; } // vector<int> list; // ListNode *listProbe = head; // while(listProbe != NULL) { // list.push_back(listProbe->val); // } return sortedListToBSTRecur(0, length-1, head); }