Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Print leftmost and rightmost nodes of a Binary Tree in C Program.
In C, printing the leftmost and rightmost nodes of a binary tree involves traversing the tree level by level and capturing the first and last nodes at each level. This technique uses level-order traversal (BFS) to identify boundary nodes.
Syntax
struct node {
int data;
struct node* left;
struct node* right;
};
void printBoundaryNodes(struct node* root);
Algorithm
The approach uses a queue-based level-order traversal −
- Process each level completely before moving to the next
- For each level, store the first node (leftmost) and last node (rightmost)
- Use a queue to maintain nodes and track level boundaries
Example
This program creates a binary tree and prints all leftmost and rightmost nodes using level-order traversal −
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
struct node* newNode(int data) {
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
void printBoundaryNodes(struct node* root) {
if (root == NULL)
return;
struct node* queue[1000];
int front = 0, rear = 0;
int result[1000], resultIndex = 0;
queue[rear++] = root;
while (front < rear) {
int levelSize = rear - front;
for (int i = 0; i < levelSize; i++) {
struct node* current = queue[front++];
/* Store first and last node of each level */
if (i == 0 || i == levelSize - 1) {
result[resultIndex++] = current->data;
}
if (current->left)
queue[rear++] = current->left;
if (current->right)
queue[rear++] = current->right;
}
}
printf("Leftmost and Rightmost nodes: ");
for (int i = 0; i < resultIndex; i++) {
printf("%d ", result[i]);
}
printf("
");
}
int main() {
struct node* root = newNode(106);
root->left = newNode(20);
root->right = newNode(320);
root->left->left = newNode(100);
root->left->right = newNode(21);
root->right->left = newNode(61);
root->right->right = newNode(52);
printBoundaryNodes(root);
return 0;
}
Leftmost and Rightmost nodes: 106 20 320 100 52
How It Works
The algorithm processes each level of the tree separately:
- Level 0: Root node (106) − both leftmost and rightmost
- Level 1: Leftmost (20), Rightmost (320)
- Level 2: Leftmost (100), Rightmost (52)
Key Points
- Uses level-order traversal with a simple array-based queue implementation
- Time complexity: O(n) where n is the number of nodes
- Space complexity: O(w) where w is the maximum width of the tree
- For a single node level, the same node is both leftmost and rightmost
Conclusion
This approach efficiently identifies boundary nodes using level-order traversal. It captures the leftmost and rightmost nodes at each level, providing a complete view of the tree's boundary structure.
Advertisements
