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

Given a binary tree. Find if a given vertical level of the binary tree is sorted or not.
(For the case when two nodes are overlapping, check if they form a sorted sequence in the level they lie.)

Prerequisite: Vertical Order Traversal


Input : 1
       / \
      2   5
     / \
    7   4
   Level l = -1   
Output : Yes
Nodes in level -1 are 2 -> 6 which
forms a sorted sequence.

Input: 1
        / \
       2   6
        \ /
        3 4
   Level l = 0
Output : Yes
Note that nodes with value 3 and 4
are overlapping in the binary tree.
So we check if this form a sorted
sequence level wise. The sequence formed
at level 0 is 1 -> 3 -> 4 which is sorted.  

A simple solution is to first do level order traversal of the binary tree and store each vertical level in different arrays. After this check, if array corresponding to level l is sorted or not. This solution has large memory requirements that can be reduced.

A efficient solution is to do vertical level order traversal of the binary tree and keep track of node values in vertical level l of the binary tree. A sequence is sorted if the previous element is less than or equal to the current element. While doing vertical level order traversal store previous value and compare current node in vertical level l with this previous value of level l. If current node value is greater than or equal to the previous value, then repeat the same procedure until the end of level l. If at any stage current node value is less than previous value then the level l is not sorted. If we reach at the end of level l then the level is sorted.






// CPP program to determine whether
// vertical level l of binary tree
// is sorted or not.
#include <bits/stdc++.h>
using namespace std;
// Structure of a tree node.
struct Node {
    int key;
    Node *left, *right;
// Function to create new tree node.
Node* newNode(int key)
    Node* temp = new Node;
    temp->key = key;
    temp->left = temp->right = NULL;
    return temp;
// Helper function to determine if
// vertical level l of given binary
// tree is sorted or not.
bool isSorted(Node* root, int level)
    // If root is null, then the answer is an
    // empty subset and an empty subset is
    // always considered to be sorted.
    if (root == NULL) 
        return true;
    // Variable to store previous
    // value in vertical level l.
    int prevVal = INT_MIN;
    // Variable to store current level
    // while traversing tree vertically.
    int currLevel;
    // Variable to store current node
    // while traversing tree vertically.
    Node* currNode;
    // 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<Node*, int> > q;
    // Insert root in queue. Vertical level
    // of root is 0.
    q.push(make_pair(root, 0));
    // Do vertical order traversal until
    // all the nodes are not visited.
    while (!q.empty()) {
        currNode = q.front().first;
        currLevel = q.front().second;
        // Check if level of node extracted from
        // queue is required level or not. If it
        // is the required level then check if
        // previous value in that level is less
        // than or equal to value of node.
        if (currLevel == level) {
            if (prevVal <= currNode->key) 
                prevVal = currNode->key;            
                return false;            
        // If left child is not NULL then push it
        // in queue with level reduced by 1.
        if (currNode->left)
            q.push(make_pair(currNode->left, currLevel - 1));
        // If right child is not NULL then push it
        // in queue with level increased by 1.
        if (currNode->right)
            q.push(make_pair(currNode->right, currLevel + 1));
    // 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   5
          / \
         7   4
    Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(5);
    root->left->left = newNode(7);
    root->left->right = newNode(4);
    root->left->right->left = newNode(6);
    int level = -1;
    if (isSorted(root, level) == true)
        cout << "Yes";
        cout << "No";
    return 0;




Time Complexity: O(n)
Auxiliary Space: O(n)

My Personal Notes arrow_drop_up

A Programmer and A Machine learning Enthusiast

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.