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


Concept

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.

(With respect of this case, when two nodes are overlapping, verify if they form a sorted sequence in the level they lie.)

Input

2
/ \
3 6
/ \
8 5
  /
7
Level l = -1

Output

Yes

Nodes in level -1 are 3 -> 7 which form a sorted sequence.

Input

2
/ \
3 7
\ /
4 5
Level l = 0

Output

Yes

It should be noted that nodes with value 4 and 5 are overlapping in the binary tree.

Now we verify if this form a sorted sequence level wise. Nodes in level 0 are 2 -> 4 -> 5 which form a sorted sequence.

Method

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.

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.

Example

 Live Demo

// CPP program to determine whether
// vertical level l of binary tree
// is sorted or not.
#include <bits/stdc++.h>
using namespace std;
// Shows structure of a tree node.
struct Node1 {
   int key1;
   Node1 *left1, *right1;
};
// Shows function to create new tree node.
Node1* newNode(int key1){
   Node1* temp1 = new Node1;
   temp1->key1 = key1;
   temp1->left1 = temp1->right1 = NULL;
   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){
   // So If root is null, then the answer is an
   // empty subset and an empty subset is
   // always considered to be sorted.
   if (root1 == NULL)
      return true;
   // Indicates variable to store previous
   // value in vertical level l.
   int prevVal1 = INT_MIN;
   // Indicates variable to store current level
   // while traversing tree vertically.
   int currLevel1;
   // Indicates variable to store current node
   // while traversing tree vertically.
   Node1* currNode1;
   // Used to declare queue to do vertical order
   // traversal. A pair is used as element
   // of queue. The first element in pair
   // represents the node and the second
   // element represents vertical level
   // of that node.
   queue<pair<Node1*, int>> q1;
   // Used to insert root in queue. Vertical level
   // of root is 0.
   q1.push(make_pair(root1, 0));
   // Perform vertical order traversal until
   // all the nodes are not visited.
   while (!q1.empty()) {
      currNode1 = q1.front().first;
      currLevel1 = q1.front().second;
      q1.pop();
      // Verify if level of node extracted from
      // queue is required level or not. If it
      // is the required level then verify if
      // previous value in that level is less
      // than or equal to value of node.
      if (currLevel1 == level1) {
         if (prevVal1 <= currNode1->key1)
            prevVal1 = currNode1->key1;
      else
         return false;
   }
   // So if left child is not NULL then push it
   // in queue with level reduced by 1.
   if (currNode1->left1)
      q1.push(make_pair(currNode1->left1, currLevel1 - 1));
   // So if right child is not NULL then push it
   // in queue with level increased by 1.
   if (currNode1->right1)
      q1.push(make_pair(currNode1->right1, currLevel1 + 1));
   }
   // So if the level asked is not present in the
   // given binary tree, that means that level
   // will contain an empty subset. Therefore answer
   // will be true.
   return true;
}
// Driver program
int main(){
/*
      2
      / \
      3 6
      / \
      8 5
        /
      7
*/
   Node1* root1 = newNode(2);
   root1->left1 = newNode(3);
   root1->right1 = newNode(6);
   root1->left1->left1 = newNode(8);
   root1->left1->right1 = newNode(5);
   root1->left1->right1->left1 = newNode(7);
   int level1 = -1;
   if (isSorted1(root1, level1) == true)
      cout << "Yes";
   else
      cout << "No";
   return 0;
}

Output

Yes

Updated on: 24-Jul-2020

47 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements