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.
C++
#include <bits/stdc++.h> using namespace std; /* A binary tree node has key, pointer to left child and a pointer to right child */struct Node { int key; struct Node* left, *right; }; /* To create a newNode of tree and return pointer */struct Node* newNode(int key) { Node* temp = new Node; temp->key = key; temp->left = temp->right = NULL; return (temp); } // Takes two parameters - same initially and // calls recursively void printMiddleLevelUtil(Node* a, Node* b) { // Base case e if (a == NULL || b == NULL) return; // Fast pointer has reached the leaf so print // value at slow pointer if ((b->left == NULL) && (b->right == NULL)) { cout << a->key << " "; return; } // Recursive call // root.left.left and root.left.right will // print same value // root.right.left and root.right.right // will print same value // So we use any one of the condition printMiddleLevelUtil(a->left, b->left->left); printMiddleLevelUtil(a->right, b->left->left); } // Main printing method that take a Tree as input void printMiddleLevel(Node* node) { printMiddleLevelUtil(node, node); } // Driver program to test above functions int main() { Node* n1 = newNode(1); Node* n2 = newNode(2); Node* n3 = newNode(3); Node* n4 = newNode(4); Node* n5 = newNode(5); Node* n6 = newNode(6); Node* n7 = newNode(7); n2->left = n4; n2->right = n5; n3->left = n6; n3->right = n7; n1->left = n2; n1->right = n3; printMiddleLevel(n1); } // This code is contributed by Prasad Kshirsagar |
Java
// Tree node definition class Node { public int key; public Node left; public Node right; public Node(int val) { this.left = null; this.right = null; this.key = val; } } public class PrintMiddle { // Takes two parameters - same initially and // calls recursively private static void printMiddleLevelUtil(Node a, Node b) { // Base case e if (a == null || b == null) return; // Fast pointer has reached the leaf so print // value at slow pointer if ((b.left == null) && (b.right == null)) { System.out.print(a.key + " "); return; } // Recursive call // root.left.left and root.left.right will // print same value // root.right.left and root.right.right // will print same value // So we use any one of the condition printMiddleLevelUtil(a.left, b.left.left); printMiddleLevelUtil(a.right, b.left.left); } // Main printing method that take a Tree as input public static void printMiddleLevel(Node node) { printMiddleLevelUtil(node, node); } public static void main(String[] args) { Node n1 = new Node(1); Node n2 = new Node(2); Node n3 = new Node(3); Node n4 = new Node(4); Node n5 = new Node(5); Node n6 = new Node(6); Node n7 = new Node(7); n2.left = n4; n2.right = n5; n3.left = n6; n3.right = n7; n1.left = n2; n1.right = n3; printMiddleLevel(n1); } } |
Output:
2 3
This article is contributed by Balkishan. You could hit me an email – kishan020696@gmail.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 contribute@geeksforgeeks.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.
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.
Recommended Posts:
- Print the middle nodes of each level of a Binary Tree
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Print the nodes corresponding to the level value for each level of a Binary Tree
- Height of binary tree considering even level leaves only
- Calculate height of Binary Tree using Inorder and Level Order Traversal
- Perfect Binary Tree Specific Level Order Traversal
- Perfect Binary Tree Specific Level Order Traversal | Set 2
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Difference between sums of odd level and even level nodes of a Binary Tree
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Nodes at Kth level without duplicates in a Binary Tree
- Print the nodes of the Binary Tree whose height is a Prime number
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Difference between sums of odd level and even level nodes in an N-ary Tree
- Middle To Up-Down Order traversal of a Binary Tree
- Postorder traversal of Binary Tree without recursion and without stack
- Print nodes between two given level numbers of a binary 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
- Print odd positioned nodes of odd levels in level order of the given binary tree
Improved By : Prasad_Kshirsagar

