Convert Sorted List to Binary Search Tree in C++

C++Server Side ProgrammingProgramming

<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 &minus;</p><p><img src="" 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 &minus;</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 &minus;</p><p><a class="demo" href="" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">#include &lt;bits/stdc++.h&gt; using namespace std; class ListNode{ &nbsp; &nbsp;public: &nbsp; &nbsp;int val; &nbsp; &nbsp;ListNode *next; &nbsp; &nbsp;ListNode(int data){ &nbsp; &nbsp; &nbsp; val = data; &nbsp; &nbsp; &nbsp; next = NULL; &nbsp; &nbsp;} }; ListNode *make_list(vector&lt;int&gt; v){ &nbsp; &nbsp;ListNode *head = new ListNode(v[0]); &nbsp; &nbsp;for(int i = 1; i&lt;v.size(); i++){ &nbsp; &nbsp; &nbsp; ListNode *ptr = head; &nbsp; &nbsp; &nbsp; while(ptr-&gt;next != NULL){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ptr = ptr-&gt;next; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; ptr-&gt;next = new ListNode(v[i]); &nbsp; &nbsp;} &nbsp; &nbsp;return head; } class TreeNode{ &nbsp; &nbsp;public: &nbsp; &nbsp;int val; &nbsp; &nbsp;TreeNode *left, *right; &nbsp; &nbsp;TreeNode(int data){ &nbsp; &nbsp; &nbsp; val = data; &nbsp; &nbsp; &nbsp; left = right = NULL; &nbsp; &nbsp;} }; void inord(TreeNode *root){ &nbsp; &nbsp;if(root != NULL){ &nbsp; &nbsp; &nbsp; inord(root-&gt;left); &nbsp; &nbsp; &nbsp; cout &lt;&lt; root-&gt;val &lt;&lt; &quot; &quot;; &nbsp; &nbsp; &nbsp; inord(root-&gt;right); &nbsp; &nbsp;} } class Solution { &nbsp; &nbsp;public: &nbsp; &nbsp;pair &lt;ListNode*, ListNode*&gt; getMid(ListNode* a){ &nbsp; &nbsp; &nbsp; ListNode* prev = NULL; &nbsp; &nbsp; &nbsp; ListNode* fast = a; &nbsp; &nbsp; &nbsp; ListNode* slow = a; &nbsp; &nbsp; &nbsp; while(fast &amp;&amp; fast-&gt;next){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fast = fast-&gt;next-&gt;next; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;prev = slow; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;slow = slow-&gt;next; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; return {prev, slow}; &nbsp; &nbsp;} &nbsp; &nbsp;TreeNode* sortedListToBST(ListNode* a) { &nbsp; &nbsp; &nbsp; if(!a)return NULL; &nbsp; &nbsp; &nbsp; pair&lt;ListNode*, ListNode*&gt; x = getMid(a); &nbsp; &nbsp; &nbsp; ListNode* mid = x.second; &nbsp; &nbsp; &nbsp; TreeNode* Node = new TreeNode(mid-&gt;val); &nbsp; &nbsp; &nbsp; ListNode* nextStart = mid-&gt;next; &nbsp; &nbsp; &nbsp; mid-&gt;next = NULL; &nbsp; &nbsp; &nbsp; Node-&gt;right = sortedListToBST(nextStart); &nbsp; &nbsp; &nbsp; if(x.first){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x.first-&gt;next = NULL; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Node-&gt;left = sortedListToBST(a); &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; return Node; &nbsp; &nbsp;} }; main(){ &nbsp; &nbsp;vector&lt;int&gt; v = {-10,-3,0,5,9}; &nbsp; &nbsp;ListNode *head = make_list(v); &nbsp; &nbsp;Solution ob; &nbsp; &nbsp;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>
Updated on 29-Apr-2020 14:16:42