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 / 6 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.
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.
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Check if a binary tree is sorted level-wise or not
- Difference between sums of odd level and even level nodes of a Binary Tree
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Print the nodes corresponding to the level value for each level of a Binary Tree
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Build Binary Tree from BST such that it's level order traversal prints sorted data
- Check if value exists in level-order sorted complete binary tree
- Find maximum vertical sum in binary tree
- Find the kth node in vertical order traversal of a Binary Tree
- Vertical Sum in a given Binary Tree | Set 1
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Difference between sums of odd level and even level nodes in an N-ary Tree
- Check whether a given binary tree is skewed binary tree or not?
- Print a Binary Tree in Vertical Order | Set 1
- Vertical width of Binary tree | Set 1
- Vertical width of Binary tree | Set 2
- Vertical Sum in Binary Tree | Set 2 (Space Optimized)
- Print a Binary Tree in Vertical Order | Set 2 (Map based Method)
- Check if max sum level of Binary tree divides tree into two equal sum halves
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.
Improved By : sanjeev2552