Check if a Binary Tree contains node values in strictly increasing and decreasing order at even and odd levels


Level of a Binary Tree − In a binary tree, the level of the node refers to its distance from the root node. The root node is considered at level 0, its immediate children are at level 1, their children at level 2 and so on.

Levels of a binary tree are explained in the following example,

        A           <- Level 0
      /   \
     B     C        <- Level 1
    / \   / \
   D   E F   G      <- Level 2

Problem Statement

Given a binary tree, check if it contains node values in strictly increasing and decreasing order at even and odd intervals.

Sample Example 1

Input

         2
      /     \
     9        7
    / \     /  \
   1   5   6    8

Output

True

Explanation

Level 1 - 9 and 7 node values are strictly decreasing

Level 2 - 1, 5, 6, and 8 node values are strictly increasing

Thus, the given tree has node values in strictly increasing and decreasing order for even and odd levels.

Sample Example 2

Input

        8
      /   \
     9     8
    / \
   4   3

Output

False

Explanation

Level 1 - 9 and 8 node values are strictly decreasing

Level 2 - 4 and 3 node values are not strictly increasing

Thus, the given tree has node values in strictly increasing and decreasing order for even and odd levels.

Solution Approach

A queue is used to perform the level order traversal of the binary tree and the queue initially has the root node in it. As the function traverses, the level of the tree is tracked starting with level 0. At each level, the node values are stored in a vector to check if they are strictly increasing or strictly decreasing or none. Then the level of the nodes is checked. If the node level is even and the node values are strictly increasing or if the level is odd and the node values are strictly decreasing, then return True else return False.

Pseudocode −

function checkEvenOddLevel(root):
   if root is NULL:
      return true    
   queue = empty queue
   enqueue root to queue
   level = 0    
   while queue is not empty:
      vec = empty vector        
      size = size of queue
        
      for i = 0 to size - 1:
         node = dequeue from queue
         add node's value to vec            
         if node has left child:
            enqueue left child to queue            
         if node has right child:
            enqueue right child to queue        
      if level is even:
         for i = 0 to size of vec - 2:
            if vec[i + 1] <= vec[i]:
               return false        
      if level is odd:
         for i = 0 to size of vec - 2:
            if vec[i + 1] >= vec[i]:
               return false        
      increment level by 1    
   return true
root = create binary tree
if checkEvenOddLevel(root):
   print "True"
else:
   print "False"

Exmaple: C++ Implementation

The following code checks if the node values are strictly increasing or decreasing for even and odd levels of the binary tree.

// C++ program for the above approach
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

struct Node {
   int val;
   Node* left, *right;
};
Node* newNode(int data) {
   Node* temp = new Node();
   temp->val = data;
   temp->left = NULL;
   temp->right = NULL;
   return temp;
}
// Function to check if given binary tree satisfies the required conditions
bool checkEvenOddLevel(Node* root) {
   if (root == NULL)
      return true;
   // Queue to store nodes of each level
   queue<Node*> q;
   q.push(root);
   // Stores the current level of the binary tree
   int level = 0;
   // Traverse until the queue is empty
   while (!q.empty()) {
      vector<int> vec;
      // Stores the number of nodes present in the current level
      int size = q.size();
      for (int i = 0; i < size; i++) {
         Node* node = q.front();
         vec.push_back(node->val);
         // Insert left and right child of node into the queue
         if (node->left != NULL)
            q.push(node->left);
         if (node->right != NULL)
            q.push(node->right);
         q.pop();
      }
      // If the level is even
      if (level % 2 == 0) {
         // If the nodes in this level are in strictly increasing order or not
         for (int i = 0; i < vec.size() - 1; i++) {
            if (vec[i + 1] > vec[i])
               continue;
            return false;
         }
      }
      // If the level is odd
      else if (level % 2 == 1) {
         // If the nodes in this level are in strictly decreasing order or not
         for (int i = 0; i < vec.size() - 1; i++) {
            if (vec[i + 1] < vec[i])
               continue;
            return false;
         }
      }
      // Increment the level count
      level++;
   }
   return true;
}
int main(){
   // Construct a Binary Tree
   Node* root = NULL;
   root = newNode(2);
   root->left = newNode(6);
   root->right = newNode(3);
   root->left->left = newNode(4);
   root->left->right = newNode(7);
   root->right->right = newNode(11);
   root->left->left->left = newNode(10);
   root->left->left->right = newNode(5);
   root->left->right->right = newNode(1);
   // Function Call
   if (checkEvenOddLevel(root))
      cout << "True";
   else
      cout << "False";
   return 0;
}

Output

True

Time Complexity − O(n) as we are traversing the entire tree once

Space Complexity − O(n) due to the space used by queue and vector.

Conclusion

In conversation, we discussed the problem of checking if a binary tree contains node values in strictly increasing and decreasing order at even and odd levels. It performs a level order traversal using a queue and checks the order of values at each level based on whether the level is even or odd. The code has a time complexity of O(N) and a space complexity of O(N), where N is the number of nodes in the binary tree. It can determine if the binary tree satisfies the required conditions efficiently and is suitable for practical use.

Updated on: 25-Oct-2023

25 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements