Given a Binary Tree, the task is to check if it consists of node values arranged in strictly increasing order at even levels and strictly decreasing at odd levels (Assuming the root node to be at level 0).
Input:2 / \ 6 3 / \ \ 4 7 11 / \ \ 10 5 1
At level 1 (odd), Node values 6 and 3 are in strictly decreasing order.
At level 2 (even), Node values 4, 7, and 11 are in strictly increasing order.
At level 3 (odd), Node values 10, 5, and 1) are in strictly decreasing order.
Therefore, the tree satisfies the given conditions.
5 / \ 6 3 / \ \ 4 9 2
Approach: The idea is to perform Level Order Traversal on the given Binary Tree and for each level, check if it satisfies the given conditions or not. Follow the steps below to solve the problem:
- Create an empty Queue to store nodes of each level one by one during the Level Order Traversal of the tree.
- Push the root node into the Queue.
- Iterate until the queue is empty and perform the following:
- Keep popping nodes of the current level from the queue and insert it into an Arraylist. Push all of its children nodes into the Queue.
- If the level is even, check if elements present in the Arraylist is in increasing order or not. If found to be true, proceed to the next level. Otherwise, print No.
- Similarly, check for the odd levels.
- After complete traversal of the tree, if all levels are found to be satisfying the conditions, print YES.
Below is the implementation of the 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.
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Print even positioned nodes of odd levels in level order of the given binary tree
- Print odd positioned nodes of even levels in level order of the given binary tree
- Print even positioned nodes of even levels in level order of the given binary tree
- Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Print all numbers in given range having digits in strictly increasing order
- Print Binary Tree levels in sorted order | Set 2 (Using set)
- Print Binary Tree levels in sorted order
- Create loops of even and odd values in a binary tree
- Check if a Binary Tree is an Even-Odd Tree or not
- Print levels with odd number of nodes and even number of nodes
- Print all the levels with odd and even number of nodes in it | Set-2
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Print updated levels of each node of a Complete Binary Tree based on difference in weights of subtrees
- Print the nodes at odd levels of a tree
- Minimum increments of Non-Decreasing Subarrays required to make Array Non-Decreasing
- Averages of Levels in Binary Tree
- Print Levels of all nodes in a Binary Tree
- Print all nodes between two given levels in Binary Tree
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.