Given a binary tree, the task is to print all the nodes except the rightmost in every level of the tree. The root is considered at level 0, and rightmost node of any level is considered as a node at position 0.
Input: 1 / \ 2 3 / \ \ 4 5 6 / \ 7 8 / \ 9 10 Output: 2 4 5 7 9 Input: 1 / \ 2 3 \ \ 4 5 Output: 2 4
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 if the node in the queue of level order is the last node then that node will be the rightmost node and don’t print that node.
Below is the implementation of the above approach:
2 4 5 6 8
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Print all the nodes except the leftmost node in every level of the given binary tree
- Print leftmost and rightmost nodes of a Binary Tree
- Print nodes between two given level numbers of a binary tree
- 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 odd 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 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 the nodes of binary tree as they become the leaf node
- Difference between sums of odd level and even level nodes of a Binary Tree
- Sum of all nodes at Kth level in a Binary Tree
- Nodes at Kth level without duplicates in a Binary Tree
- Swap Nodes in Binary tree of every k'th level
- Count nodes with two children at level L in a Binary Tree
- Get Level of a node in a Binary Tree
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Depth of the deepest odd level node in Binary Tree
- Find the maximum node at a given level in a binary tree
- Get level of a node in binary tree | iterative approach
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.