#include <stdio.
h>
#include <stdlib.h>
// Definition of a binary tree node
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
// Recursive inorder traversal function
void inorderTraversal(struct Node* root) {
if (root == NULL)
return;
inorderTraversal(root->left); // Traverse left subtree
printf("%d ", root->data); // Visit node
inorderTraversal(root->right); // Traverse right subtree
}
int main() {
struct Node* root = createNode(4);
root->left = createNode(2);
root->right = createNode(5);
root->left->left = createNode(1);
root->left->right = createNode(3);
printf("Inorder Traversal: ");
inorderTraversal(root);
printf("\n");
return 0;
}
Output: 1 2 3 4 5
/ Example tree:
4
/\
2 5
/\
1 3
#include <stdio.h>
#include <stdlib.h>
// Definition of a binary tree node
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
// Preorder traversal: Root -> Left -> Right
void preorderTraversal(struct Node* root) {
if (root == NULL)
return;
printf("%d ", root->data); // Visit root node
preorderTraversal(root->left); // Traverse left subtree
preorderTraversal(root->right); // Traverse right subtree
}
int main() {
struct Node* root = createNode(4);
root->left = createNode(2);
root->right = createNode(5);
root->left->left = createNode(1);
root->left->right = createNode(3);
printf("Preorder Traversal: ");
preorderTraversal(root);
printf("\n");
return 0;
} // Output: 4 2 1 3 5
// Same example tree:
// 4
// / \
// 2 5
// / \
// 1 3
#include <stdio.h>
#include <stdlib.h>
// Definition of a binary tree node
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
// Inorder traversal: Left -> Root -> Right
void inorderTraversal(struct Node* root) {
if (root == NULL)
return;
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
// Preorder traversal: Root -> Left -> Right
void preorderTraversal(struct Node* root) {
if (root == NULL)
return;
printf("%d ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
// Postorder traversal: Left -> Right -> Root
void postorderTraversal(struct Node* root) {
if (root == NULL)
return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->data);
}
int main() {
// Example tree:
// 4
// / \
// 2 5
// / \
// 1 3
struct Node* root = createNode(4);
root->left = createNode(2);
root->right = createNode(5);
root->left->left = createNode(1);
root->left->right = createNode(3);
printf("Inorder Traversal: ");
inorderTraversal(root); // Output: 1 2 3 4 5
printf("\n");
printf("Preorder Traversal: ");
preorderTraversal(root); // Output: 4 2 1 3 5
printf("\n");
printf("Postorder Traversal: ");
postorderTraversal(root); // Output: 1 3 2 5 4
printf("\n");
return 0;
}