Given a perfect binary tree, print nodes of middle level without computing its height. A perfect binary tree is a binary tree in which all interior nodes have two children and all leaves have the same depth or same level.
Output : 4 5 6 7
The idea is similar to method 2 of finding middle of singly linked list.
Use fast and slow (or tortoise) pointers in each route of a the tree.
1. Advance fast pointer towards leaf by 2.
2. Advance slow pointer towards lead by 1.
3. If fast pointer reaches the leaf print value at slow pointer
4. Call recursively the next route.
This article is contributed by Balkishan. You could hit me an email – email@example.com 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Height of binary tree considering even level leaves only
- Perfect Binary Tree Specific Level Order Traversal | Set 2
- Perfect Binary Tree Specific Level Order Traversal
- Calculate height of Binary Tree using Inorder and Level Order Traversal
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- 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 odd 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
- 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
- Recursive Program to Print extreme nodes of each level of Binary Tree in alternate order
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Difference between sums of odd level and even level nodes of a Binary Tree
Improved By : Prasad_Kshirsagar