Given a Binary Tree, the task is to check if the binary tree is an Even-Odd binary tree or not.
A Binary Tree is called an Even-Odd Tree when all the nodes which are at even levels have even values (assuming root to be at level 0) and all the nodes which are at odd levels have odd values.
Input:2 / \ 3 9 / \ \ 4 10 6
Only node on level 0 (even) is 2 (even).
Nodes present in level 1 are 3 and 9 (both odd).
Nodes present in level 2 are 4, 10 and 6 (all even).
Therefore, the Binary tree is an odd-even binary tree.
Input:4 / \ 3 7 / \ \ 4 10 5
Approach: Follow the steps below to solve the problem:
- The idea is to perform level-order traversal and check if the nodes present on even levels are even valued or not and nodes present on odd levels are odd valued or not.
- If any node at an odd level is found to have odd value or vice-versa, then print “NO“.
- Otherwise, after complete traversal of the tree, 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.
- Check whether a binary tree is a full binary tree or not
- Check whether a given binary tree is skewed binary tree or not?
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- 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
- Complexity of different operations in Binary tree, Binary Search Tree and AVL 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 is sorted level-wise or not
- Check if Inorder traversal of a Binary Tree is palindrome or not
- Check if a Binary Tree (not BST) has duplicate values
- Binary Tree to Binary Search Tree Conversion
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Minimum swap required to convert binary tree to binary search tree
- Difference between Binary Tree and Binary Search 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 firstname.lastname@example.org. 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.