Find if given vertical level of binary tree is sorted or not in C++

C++Server Side ProgrammingProgramming

<h2>Concept</h2><p>With respect of a given binary tree, our task is to determine if a given vertical level of the binary tree is sorted or not.</p><p>(With respect of this case, when two nodes are overlapping, verify if they form a sorted sequence in the level they lie.)</p><h2>Input</h2><pre class="result notranslate">2 / \ 3 6 / \ 8 5 &nbsp; / 7 Level l = -1</pre><h2>Output</h2><pre class="result notranslate">Yes</pre><p>Nodes in level -1 are 3 -&gt; 7 which form a sorted sequence.</p><h2>Input</h2><pre class="result notranslate">2 / \ 3 7 \ / 4 5 Level l = 0</pre><h2>Output</h2><pre class="result notranslate">Yes</pre><p>It should be noted that nodes with value 4 and 5 are overlapping in the binary tree.</p><p>Now we verify if this form a sorted sequence level wise. Nodes in level 0 are 2 -&gt; 4 -&gt; 5 which form a sorted sequence.</p><h2>Method</h2><p>According to simple solution,at first we perform level order traversal of the binary treeand store each vertical level in different arrays. In this case, we verify, if array corresponding to level l is sorted or not. It should be noted that this solution has large memory requirements that can be reduced.</p><p>According to efficient solution,we perform vertical level order traversal of the binary tree and maintain track of node values in vertical level l of the binary tree. A sorted sequence is generated if the previous element is smaller than or equal to the current element. At the time of performing vertical level order traversal, store previous value and compare current node in vertical level l with this previous value of level l. It has beenseen that if current node value is larger than or equal to the previous value, then we have to repeat the same procedure until the end of level l. It has been seen that if at any stage current node value is smaller than previous value then the level l is not sorted. Again it has been observed that if we reach at the end of level l then the level is sorted.</p><h2>Example</h2><p><a class="demo" href=" " rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">// CPP program to determine whether // vertical level l of binary tree // is sorted or not. #include &lt;bits/stdc++.h&gt; using namespace std; // Shows structure of a tree node. struct Node1 { &nbsp; &nbsp;int key1; &nbsp; &nbsp;Node1 *left1, *right1; }; // Shows function to create new tree node. Node1* newNode(int key1){ &nbsp; &nbsp;Node1* temp1 = new Node1; &nbsp; &nbsp;temp1-&gt;key1 = key1; &nbsp; &nbsp;temp1-&gt;left1 = temp1-&gt;right1 = NULL; &nbsp; &nbsp;return temp1; } // Indicates helper function to determine if // vertical level l of given binary // tree is sorted or not. bool isSorted1(Node1* root1, int level1){ &nbsp; &nbsp;// So If root is null, then the answer is an &nbsp; &nbsp;// empty subset and an empty subset is &nbsp; &nbsp;// always considered to be sorted. &nbsp; &nbsp;if (root1 == NULL) &nbsp; &nbsp; &nbsp; return true; &nbsp; &nbsp;// Indicates variable to store previous &nbsp; &nbsp;// value in vertical level l. &nbsp; &nbsp;int prevVal1 = INT_MIN; &nbsp; &nbsp;// Indicates variable to store current level &nbsp; &nbsp;// while traversing tree vertically. &nbsp; &nbsp;int currLevel1; &nbsp; &nbsp;// Indicates variable to store current node &nbsp; &nbsp;// while traversing tree vertically. &nbsp; &nbsp;Node1* currNode1; &nbsp; &nbsp;// Used to declare queue to do vertical order &nbsp; &nbsp;// traversal. A pair is used as element &nbsp; &nbsp;// of queue. The first element in pair &nbsp; &nbsp;// represents the node and the second &nbsp; &nbsp;// element represents vertical level &nbsp; &nbsp;// of that node. &nbsp; &nbsp;queue&lt;pair&lt;Node1*, int&gt;&gt; q1; &nbsp; &nbsp;// Used to insert root in queue. Vertical level &nbsp; &nbsp;// of root is 0. &nbsp; &nbsp;q1.push(make_pair(root1, 0)); &nbsp; &nbsp;// Perform vertical order traversal until &nbsp; &nbsp;// all the nodes are not visited. &nbsp; &nbsp;while (!q1.empty()) { &nbsp; &nbsp; &nbsp; currNode1 = q1.front().first; &nbsp; &nbsp; &nbsp; currLevel1 = q1.front().second; &nbsp; &nbsp; &nbsp; q1.pop(); &nbsp; &nbsp; &nbsp; // Verify if level of node extracted from &nbsp; &nbsp; &nbsp; // queue is required level or not. If it &nbsp; &nbsp; &nbsp; // is the required level then verify if &nbsp; &nbsp; &nbsp; // previous value in that level is less &nbsp; &nbsp; &nbsp; // than or equal to value of node. &nbsp; &nbsp; &nbsp; if (currLevel1 == level1) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (prevVal1 &lt;= currNode1-&gt;key1) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prevVal1 = currNode1-&gt;key1; &nbsp; &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return false; &nbsp; &nbsp;} &nbsp; &nbsp;// So if left child is not NULL then push it &nbsp; &nbsp;// in queue with level reduced by 1. &nbsp; &nbsp;if (currNode1-&gt;left1) &nbsp; &nbsp; &nbsp; q1.push(make_pair(currNode1-&gt;left1, currLevel1 - 1)); &nbsp; &nbsp;// So if right child is not NULL then push it &nbsp; &nbsp;// in queue with level increased by 1. &nbsp; &nbsp;if (currNode1-&gt;right1) &nbsp; &nbsp; &nbsp; q1.push(make_pair(currNode1-&gt;right1, currLevel1 + 1)); &nbsp; &nbsp;} &nbsp; &nbsp;// So if the level asked is not present in the &nbsp; &nbsp;// given binary tree, that means that level &nbsp; &nbsp;// will contain an empty subset. Therefore answer &nbsp; &nbsp;// will be true. &nbsp; &nbsp;return true; } // Driver program int main(){ /* &nbsp; &nbsp; &nbsp; 2 &nbsp; &nbsp; &nbsp; / \ &nbsp; &nbsp; &nbsp; 3 6 &nbsp; &nbsp; &nbsp; / \ &nbsp; &nbsp; &nbsp; 8 5 &nbsp; &nbsp; &nbsp; &nbsp; / &nbsp; &nbsp; &nbsp; 7 */ &nbsp; &nbsp;Node1* root1 = newNode(2); &nbsp; &nbsp;root1-&gt;left1 = newNode(3); &nbsp; &nbsp;root1-&gt;right1 = newNode(6); &nbsp; &nbsp;root1-&gt;left1-&gt;left1 = newNode(8); &nbsp; &nbsp;root1-&gt;left1-&gt;right1 = newNode(5); &nbsp; &nbsp;root1-&gt;left1-&gt;right1-&gt;left1 = newNode(7); &nbsp; &nbsp;int level1 = -1; &nbsp; &nbsp;if (isSorted1(root1, level1) == true) &nbsp; &nbsp; &nbsp; cout &lt;&lt; &quot;Yes&quot;; &nbsp; &nbsp;else &nbsp; &nbsp; &nbsp; cout &lt;&lt; &quot;No&quot;; &nbsp; &nbsp;return 0; }</pre><h2>Output</h2><pre class="result notranslate">Yes</pre>
Updated on 24-Jul-2020 11:20:26