

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Sorted List to Binary Search Tree in C++
<p>Suppose we have a singly linked list where elements are sorted in ascending order, we have to convert it to a height balanced BST. So if the list is like [-10, -3, 0, 5, 9], The possible tree will be like −</p><p><img src="https://www.tutorialspoint.com/assets/questions/media/36463/sorted_list_btn.jpg" class="fr-fic fr-dib" style="width: 218px; height: 191.927px;" width="218" height="191.927"></p><p>To solve this, we will follow these steps −</p><ul class="list"><li><p>If the list is empty, then return null</p></li><li><p>Define a recursive method called sortedListToBST() this will take list start node</p></li><li><p>x := address of the previous node of mid node from list a</p></li><li><p>mid := exact mid node</p></li><li><p>create a new node with value by taking from the value of mid</p></li><li><p>nextStart := next of mid node</p></li><li><p>set mid next as null</p></li><li><p>right of node := sortedListToBST(nextStart)</p></li><li><p>if x is not null, then next of x = null and left of node := sortedListToBST(a)</p></li><li><p>return node</p></li></ul><h2>Example (C++)</h2><p>Let us see the following implementation to get better understanding −</p><p><a class="demo" href="http://tpcg.io/oIMpYv1I" rel="nofollow" target="_blank"> Live Demo</a></p><pre class="prettyprint notranslate">#include <bits/stdc++.h> using namespace std; class ListNode{ public: int val; ListNode *next; ListNode(int data){ val = data; next = NULL; } }; ListNode *make_list(vector<int> v){ ListNode *head = new ListNode(v[0]); for(int i = 1; i<v.size(); i++){ ListNode *ptr = head; while(ptr->next != NULL){ ptr = ptr->next; } ptr->next = new ListNode(v[i]); } return head; } class TreeNode{ public: int val; TreeNode *left, *right; TreeNode(int data){ val = data; left = right = NULL; } }; void inord(TreeNode *root){ if(root != NULL){ inord(root->left); cout << root->val << " "; inord(root->right); } } class Solution { public: pair <ListNode*, ListNode*> getMid(ListNode* a){ ListNode* prev = NULL; ListNode* fast = a; ListNode* slow = a; while(fast && fast->next){ fast = fast->next->next; prev = slow; slow = slow->next; } return {prev, slow}; } TreeNode* sortedListToBST(ListNode* a) { if(!a)return NULL; pair<ListNode*, ListNode*> x = getMid(a); ListNode* mid = x.second; TreeNode* Node = new TreeNode(mid->val); ListNode* nextStart = mid->next; mid->next = NULL; Node->right = sortedListToBST(nextStart); if(x.first){ x.first->next = NULL; Node->left = sortedListToBST(a); } return Node; } }; main(){ vector<int> v = {-10,-3,0,5,9}; ListNode *head = make_list(v); Solution ob; inord(ob.sortedListToBST(head)); }</pre><h2>Input</h2><pre class="result notranslate">[-10,-3,0,5,9]</pre><h2>Output</h2><pre class="result notranslate">-10 -3 0 5 9</pre>
- Related Questions & Answers
- Convert Sorted Array to Binary Search Tree in Python
- Program to convert binary search tree to a singly linked list in C++?
- Program to convert a linked list into a binary search tree in C++
- Binary Tree to Binary Search Tree Conversion in C++
- Optimal Binary Search Tree
- Program to create linked list to binary search tree in Python
- Binary Search Tree in Javascript
- Binary Search Tree to Greater Sum Tree in C++
- Binary Tree to Binary Search Tree Conversion using STL set C++?
- Binary Search Tree Class in Javascript
- Binary Search Tree Iterator in C++
- Validate Binary Search Tree in Python
- Recover Binary Search Tree in C++
- Binary Search Tree - Search and Insertion Operations in C++
- Program to convert linked list to zig-zag binary tree in Python
Advertisements