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
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 all the nodes except the leftmost node in every level of the given binary tree
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Print leftmost and rightmost nodes of a Binary Tree
- Difference between sums of odd level and even level nodes of a Binary Tree
- Swap Nodes in Binary tree of every k'th level
- Change a Binary Tree so that every node stores sum of all nodes in left subtree
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree
- Difference between sums of odd level and even level nodes in an N-ary Tree
- Total sum except adjacent of a given node in a Binary Tree
- Print nodes between two given level numbers of a binary tree
- Print extreme nodes of each level of Binary Tree in alternate order
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Recursive Program to Print extreme nodes of each level of Binary Tree in alternate order
- Print even 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 odd positioned nodes of even levels in level order of the given binary tree
- Sum of all nodes at Kth level in a Binary Tree
- Connect Nodes at same Level (Level Order Traversal)
- Print the nodes of binary tree as they become the leaf node
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.