Given a binary tree. The task is to check if the binary tree is sorted level-wise or not. A binary tree is level sorted if max( i-1th level) is less than min( ith level ).
Input : 1 / \ / \ 2 3 / \ / \ / \ / \ 4 5 6 7 Output : Sorted Input: 1 / 4 / \ 6 5 \ 2 Output: Not sorted
Simple Solution: A simple solution is to compare minimum and maximum value of each adjacent level i and i+1. Traverse to ith and i+1th level, compare the minimum value of i+1th level with maximum value of ith level and return the result.
Time complexity: O(n2).
Efficient Solution: An efficient solution is to do level order traversal and keep track of the minimum and maximum values of current level. Use a variable prevMax to store the maximum value of the previous level. Then compare the minimum value of current level with the maximum value of the previous level, pevMax. If minimum value is greater than the prevMax, then the given tree is sorted level-wise up to current level. For next level, prevMax is the equal to maximum value of current level. So update the prevMax with maximum value of current level. Repeat this until all levels of given tree are not traversed.
Below is the implementation of above approach:
Time Complexity: O(n)
Auxiliary Space: O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Levelwise Alternating GCD and LCM of nodes in Segment Tree
- Check whether a binary tree is a full binary tree or not
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Check whether a given binary tree is skewed binary tree or not?
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Find if given vertical level of binary tree is sorted or not
- Check if a binary tree is subtree of another binary tree | Set 1
- Check if a binary tree is subtree of another binary tree | Set 2
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Check if the Left View of the given tree is sorted or not
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Check if value exists in level-order sorted complete binary tree
- A program to check if a binary tree is BST or not
- Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
- Check whether a given binary tree is perfect or not
- Check if an array represents Inorder of Binary Search tree or not
- Check if a Binary Tree (not BST) has duplicate values
- Check if Inorder traversal of a Binary Tree is palindrome or not
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.