Given a binary tree, the task is to print the even positioned nodes of odd levels in the level order traversal of the tree. The root is considered at level 0, and the leftmost node of any level is considered as a node at position 0.
Input: 1 / \ 2 3 / \ / \ 4 5 6 7 / \ 8 9 / \ 10 11 Output: 2 8 Input: 2 / \ 4 15 / / 45 17 Output: 4
Prerequisite – Even positioned elements at even level
Approach: To print nodes level by level, use level order traversal. The idea is based on Print level order traversal line by line. For that, traverse nodes level by level and switch odd level flag after every level. Similarly, mark 1st node in every level as even position and switch it after each time the next node is processed.
Below is the implementation of the above approach:
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 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
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Difference between sums of odd level and even level nodes of a Binary Tree
- Print levels with odd number of nodes and even number of nodes
- Check if a Binary Tree contains node values in strictly increasing and decreasing order at even and odd levels
- Difference between sums of odd level and even level nodes in an N-ary Tree
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Print all the levels with odd and even number of nodes in it | Set-2
- Print the nodes corresponding to the level value for each level of a Binary Tree
- Difference between sums of odd position and even position nodes for each level of a Binary Tree
- Print the nodes at odd levels of a tree
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Print extreme nodes of each level of Binary Tree in alternate order
- Recursive Program to Print extreme nodes of each level of Binary Tree in alternate order
- Print all nodes between two given levels in Binary Tree
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Print Binary Tree levels in sorted order | Set 2 (Using set)
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 : 29AjayKumar