Print middle level of perfect binary tree without finding height in C language

In C, printing the middle level of a perfect binary tree without calculating its height is achieved using a two-pointer technique. This approach uses two traversal pointers moving at different speeds to identify when we reach the middle level.

A perfect binary tree is one where all interior nodes have exactly two children and all leaf nodes are at the same level.

12 21 32 41 59 33 70 Middle level: 21, 32

Syntax

void middle(struct Node* slow, struct Node* fast);
void mid_level(struct Node* root);

Algorithm

The algorithm uses two pointers − one moving one level at a time (slow) and another moving two levels at a time (fast). When the fast pointer reaches leaf nodes, the slow pointer will be at the middle level.

Example

#include <stdio.h>
#include <stdlib.h>

struct Node {
    int key;
    struct Node* left;
    struct Node* right;
};

struct Node* newNode(int value) {
    struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
    temp->key = value;
    temp->left = temp->right = NULL;
    return temp;
}

void middle(struct Node* slow, struct Node* fast) {
    if (slow == NULL || fast == NULL)
        return;
    
    /* When fast pointer reaches leaf level, slow is at middle */
    if ((fast->left == NULL) && (fast->right == NULL)) {
        printf("%d ", slow->key);
        return;
    }
    
    /* Move slow one level, fast two levels */
    middle(slow->left, fast->left->left);
    middle(slow->right, fast->left->left);
}

void mid_level(struct Node* root) {
    printf("Middle level nodes: ");
    middle(root, root);
    printf("<br>");
}

int main() {
    /* Creating perfect binary tree */
    struct Node* root = newNode(12);
    struct Node* n2 = newNode(21);
    struct Node* n3 = newNode(32);
    struct Node* n4 = newNode(41);
    struct Node* n5 = newNode(59);
    struct Node* n6 = newNode(33);
    struct Node* n7 = newNode(70);
    
    /* Building tree structure */
    root->left = n2;
    root->right = n3;
    n2->left = n4;
    n2->right = n5;
    n3->left = n6;
    n3->right = n7;
    
    mid_level(root);
    
    /* Free allocated memory */
    free(n4); free(n5); free(n6); free(n7);
    free(n2); free(n3); free(root);
    
    return 0;
}
Middle level nodes: 21 32

How It Works

  • The slow pointer moves one level down (slow->left, slow->right)
  • The fast pointer moves two levels down (fast->left->left)
  • When fast reaches leaf nodes, slow is exactly at the middle level
  • This avoids calculating the total height of the tree

Time Complexity

Operation Time Complexity Space Complexity
Middle Level Traversal O(n/2) O(log n)

Conclusion

This two-pointer approach efficiently finds the middle level without calculating height. It works specifically for perfect binary trees where all levels are completely filled.

Updated on: 2026-03-15T11:56:49+05:30

304 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements