0% found this document useful (0 votes)
5 views2 pages

C Program for Postorder Tree Traversal

Uploaded by

mathsforlife969
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

C Program for Postorder Tree Traversal

Uploaded by

mathsforlife969
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd

Abinayashri S

Write a C program that use recursive functions to traverse the given binary tree in
Postorder
program:
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


int data;
struct Node* left;
struct Node* right;
} Node;

Node* createNode(int data) {


Node* newNode = malloc(sizeof(Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}

void postorderTraversal(Node* root) {


if (root == NULL)
return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->data);
}

int main() {
Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);

int choice;
while (1) {
printf("\nBinary Tree Traversal:\n");
printf("1. Postorder Traversal\n");
printf("2. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Postorder Traversal: ");
postorderTraversal(root);
printf("\n");
break;
case 2:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
}
output:
Binary Tree Traversal:
1. Postorder Traversal
2. Exit
Enter your choice: 1
Postorder Traversal: 4 5 2 3 1

Binary Tree Traversal:


1. Postorder Traversal
2. Exit
Enter your choice: 2
Exiting...

You might also like