- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a binary tree is sorted levelwise or not in C++
Here we will see how to check a binary tree is level wise sorted or not. The level wise sorted binary tree will be look like below −
In each level, the nodes are sorted from left to right, and each layer contains higher value than its previous level.
We can solve this problem by performing level order traversal, and keep track of the minimum and maximum elements of current level. Use another variable prev_max to hold maximum value of the previous level. Then compare the minimum value of current level and maximum value of previous level prev_max. If min value is greater than prev_max, then the tree is sorted level wise up to current level. Then update prev_max and update the max value of current level. Repeat this until all levels are not traversed.
Example
#include <iostream> #include <queue> using namespace std; class Node { public: int key; Node *left, *right; }; Node* getNode(int key) { Node* newNode = new Node; newNode->key = key; newNode->left = newNode->right = NULL; return newNode; } bool isLevelWiseSorted(Node* root) { int prevMax = INT_MIN; int min_val, max_val; int levelSize; queue<Node*> q; q.push(root); while (!q.empty()) { levelSize = q.size(); min_val = INT_MAX; max_val = INT_MIN; while (levelSize > 0) { root = q.front(); q.pop(); levelSize--; min_val = min(min_val, root->key); max_val = max(max_val, root->key); if (root->left) q.push(root->left); if (root->right) q.push(root->right); } if (min_val <= prevMax) return false; prevMax = max_val; } return true; } int main() { Node* root = getNode(1); root->left = getNode(2); root->right = getNode(3); root->left->left = getNode(4); root->left->right = getNode(5); root->right->left = getNode(6); root->right->right = getNode(7); if (isLevelWiseSorted(root)) cout << "Tree is levelwise Sorted"; else cout << "Tree is Not levelwise sorted"; }
Output
Tree is level wise Sorted
- Related Articles
- Check if a binary tree is sorted level-wise or not in C++
- Find if given vertical level of binary tree is sorted or not in C++
- A program to check if a binary tree is BST or not in C ?
- C++ program to Check if a Given Binary Tree is an AVL Tree or Not
- C++ Program to Check Whether a given Binary Tree is a Full Binary Tree or not
- Find if given vertical level of binary tree is sorted or not in Python
- Check if a Tree is Isomorphic or not in C++
- Check if a given array is pairwise sorted or not in C++
- Check if a binary tree is subtree of another binary tree in C++
- Check if list is sorted or not in Python
- Check if a given tree graph is linear or not in C++
- Check if a Binary Tree (not BST) has duplicate value in C++
- Check if a given graph is tree or not
- Check if a given Binary Tree is SumTree in C++
- Check if a given Binary Tree is Heap in C++
