Print even positioned nodes of even levels in level order of the given binary tree
Given a binary tree, print even positioned nodes of even level in level order traversal. The root is considered at level 0, and the left most node of any level is considered as a node at position 0.
Examples:
Input:
1
/ \
2 3
/ \ \
4 5 6
/ \
7 8
/ \
9 10
Output: 1 4 6 9
Input:
2
/ \
4 15
/ /
45 17
Output: 2 45
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 even 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:
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; struct Node { int data; Node *left, *right; }; // Iterative method to do level order // traversal line by line void printEvenLevelEvenNodes(Node* root) { // Base Case if (root == NULL) return; // Create an empty queue for level // order traversal queue<Node*> q; // Enqueue root and initialize level as even q.push(root); bool evenLevel = true; while (1) { // nodeCount (queue size) indicates // number of nodes in the current level int nodeCount = q.size(); if (nodeCount == 0) break; // Mark 1st node as even positioned bool evenNodePosition = true; // Dequeue all the nodes of current level // and Enqueue all the nodes of next level while (nodeCount > 0) { Node* node = q.front(); // Print only even positioned // nodes of even levels if (evenLevel && evenNodePosition) cout << node->data << " "; q.pop(); if (node->left != NULL) q.push(node->left); if (node->right != NULL) q.push(node->right); nodeCount--; // Switch the even position flag evenNodePosition = !evenNodePosition; } // Switch the even level flag evenLevel = !evenLevel; } } // Utility method to create a node struct Node* newNode(int data) { struct Node* node = new Node; node->data = data; node->left = node->right = NULL; return (node); } // Driver code int main() { struct Node* root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); root->right->left = newNode(6); root->right->right = newNode(7); root->left->right->left = newNode(8); root->left->right->right = newNode(9); root->left->right->right->right = newNode(10); printEvenLevelEvenNodes(root); return 0; } |
1 4 6 10
Recommended Posts:
- 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
- Print Levels of all nodes in a Binary Tree
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Print Binary Tree levels in sorted order
- Print Binary Tree levels in sorted order | Set 2 (Using set)
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Print nodes between two given level numbers of a binary tree
- Print all the nodes except the leftmost node in every level of the given binary tree
- Print the nodes at odd levels of a tree
- Difference between sums of odd level and even level nodes of a Binary Tree
- Flatten Binary Tree in order of Level Order Traversal
- Maximum sum of leaf nodes among all levels of the given binary tree
- Maximum sum of non-leaf nodes among all levels of the given 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 contribute@geeksforgeeks.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.



