Check if the Left View of the given tree is sorted or not


In this problem, we will check whether the left view of the binary tree is sorted.

The left view of the binary tree means nodes we can see when we look at the binary tree from the left side. In simple terms, we can see only the first node of each level. So, we need to extract the value of the first node and check whether they are sorted to get the output.

Problem Statement

We have given a binary tree. We need to print whether the binary tree's left view is sorted. If it is sorted, print 'Yes'. Otherwise, print 'No' in the output.

Sample Examples

Input

  9
 /  \
13   2

Output

Yes

Explanation

The left view of the binary tree is [9, 13], which is sorted in increasing order.

Input

      5
    /   \
   20    10
  /  \  /   \
25  10 5    42

Output

Yes

Explanation

The left view of the tree is [5, 20, 25].

Input

 5
  \
   10
  /   \
 5    42

Output

No

Explanation

The left view of the tree is [5, 10, 5].

Approach

In this approach, we will use the level order traversal algorithm to traverse each binary tree level. We will store each node of the level in the queue. The first node of the queue is the left node of the current level, and we will check whether the left node of the current level is greater than the left node of the previous level.

Algorithm

  • Step 1 − Define the TreeNode, representing the structure of the tree. Also, Define the createNewNode() function to create a new node of a binary tree and construct a binary tree using tree nodes.

  • Step 2 − Define the 'que' named queue to store the tree nodes. Also, insert the head node into the queue.

  • Step 3 − Initialize the 'isSorted' variable with the true boolean value. Also, Initialize the p and q with -1.

  • Step 4 − Traverse the queue while it is not empty.

  • Step 5 − Use a nested loop to traverse each queue element.

  • Step 5.1 − Remove the front element from the queue. If p is -1, store the data of the element in the q.

  • Step 5.2 − If p is -2, and q is less than the current node's data, update q with the current node's data and p with -3. Otherwise, the update isSorted with false and breaks the loop.

    Here p = -1 means the first node of the tree. If p is -2, it means the current node is the first node of the current level. If p is -3, the current node is not the first node of the current level. So we don't need to check it.

  • Step 5.3 − If the left and right child of the current node exists, insert them into the queue. Also, decrement the length of the queue by 1, and remove the first node.

  • Step 6 − Update p to -2.

  • Step 7 − If isSorted is false in the outer loop, break the loop.

  • Step 8 − At last, print the answer based on the 'isSorted' boolean value.

Example

#include <bits/stdc++.h>
using namespace std;

// Binary Tree Node
struct TreeNode {
   int data;
   struct TreeNode *right, *left;
};
struct TreeNode *createNewNode(int key) {
   struct TreeNode *temp = new TreeNode;
   temp->data = key;
   temp->right = NULL;
   temp->left = NULL;
   return temp;
}
void CheckIfLeftSorted(TreeNode *head) {
   queue<TreeNode *> que;
   // To track whether the left view is sorted
   bool isSorted = true;
   que.push(head);
   int p = -1, q = -1;
   // BFS algorithm
   while (!que.empty()) {
      int len = que.size();
      // Traverse each level nodes
      while (len > 0) {
         head = que.front();
         // variable for initial level
         if (p == -1) {
            q = head->data;
         }
         // Logic to check whether the left view is sorted
         if (p == -2) {
            if (q <= head->data) {
               q = head->data;
               p = -3;
            } else {
               isSorted = false;
               break;
            }
         }
         // Insert the left child node in the queue
         if (head->left != NULL) {
            que.push(head->left);
         }
         // Insert the right child node in the queue
         if (head->right != NULL) {
            que.push(head->right);
         }
         len = len - 1;
         que.pop();
      }
      p = -2;
      // When the value is not sorted
      if (isSorted == false) {
         break;
      }
   }
   if (isSorted)
      cout << "Yes, the left view of the tree is sorted!" << endl;
   else
      cout << "No, the left view of the tree is not sorted!" << endl;
}
int main() {
   struct TreeNode *head = createNewNode(5);
   head->left = createNewNode(20);
   head->left->left = createNewNode(25);
   head->right = createNewNode(10);
   head->right->left = createNewNode(5);
   head->right->right = createNewNode(42);
   head->left->right = createNewNode(10);
   CheckIfLeftSorted(head);
   return 0;
}

Output

Yes, the left view of the tree is sorted!
  • Time complexity − O(N), as we traverse each node of the tree.

  • Space complexity − O(N), as we store each node of the tree in the queue.

Conclusion

Here, we learned to check whether the left view of the tree is sorted in increasing order. However, programmers can also check whether the left view is sorted in decreasing order. To check whether the right view of the tree is shorted, programmers should compare the last node of each level.

Updated on: 25-Aug-2023

53 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements