
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Print Postorder traversal from given Inorder and Preorder traversals
Given with inorder and preorder of a tree program must find the postroder traversal and print the same
Input: Inorder traversal in[] = {4, 2, 5, 1, 3, 6} Preorder traversal pre[] = {1, 2, 4, 5, 3, 6} Output: Postorder traversal post[] = {4, 5, 2, 6, 3, 1}
Algorithm
START Step 1 -> declare function as find_value(int p, int in_order[], int n) Loop For i=0 and i<n and ++i IF in_order[i]==p Return i End IF End Step 2 -> declare function as postorder(int pre_order[], int in_order[], int n) Declare int variable as root = find_value(pre_order[0], in_order, n) IF root!=0 Call postorder(pre_order+1, in_order, root) End IF root !=n-1 Call postorder(pre_order+root+1, in_order+root+1,n-root-1) End Print pre_order[0] End Step 3 -> goto main() Declare int pre_order[] = {1, 2, 4, 5, 3, 6} Declare int in_order[] = {4, 2, 5, 1, 3, 6} Declare int size = sizeof(pre_order)/sizeof(pre_order[0]) Call postorder(pre_order, in_order, size) STOP
Example
#include <stdio.h> int find_value(int p, int in_order[], int n) { for (int i = 0; i < n; ++i) { if (in_order[i] == p) { return i; } } return -1; } int postorder(int pre_order[], int in_order[], int n) { int root = find_value(pre_order[0], in_order, n); if(root !=0 ) postorder(pre_order+1, in_order, root); if (root != n-1) postorder(pre_order+root+1, in_order+root+1, n-root-1); printf("%d ", pre_order[0]); } int main(int argc, char const *argv[]) { int pre_order[] = {1, 2, 4, 5, 3, 6}; int in_order[] = {4, 2, 5, 1, 3, 6}; int size = sizeof(pre_order)/sizeof(pre_order[0]); postorder(pre_order, in_order, size); return 0; }
Output
if we run the above program then it will generate the following output
4 5 2 6 3 1
- Related Articles
- Preorder from Inorder and Postorder traversals in C++
- Construct Tree from given Inorder and Preorder traversals in C++
- Find postorder traversal of BST from preorder traversal in C++
- Construct Binary Tree from Preorder and Postorder Traversal in Python
- Construct Binary Tree from Inorder and Postorder Traversal in Python
- Construct Binary Tree from Preorder and Inorder Traversal in Python
- Program to generate tree using preorder and inorder traversal in python
- Construct BST from given preorder traversal - Set 1 in C++
- Construct BST from given preorder traversal - Set 2 in C++
- Python Program to Build Binary Tree if Inorder or Postorder Traversal as Input
- Construct Special Binary Tree from given Inorder traversal in C++
- Construct a BST from given postorder traversal using Stack in Python
- Construct a Binary Tree from Postorder and Inorder in Python
- Recover a Tree From Preorder Traversal in C++
- Construct Binary Search Tree from Preorder Traversal in Python

Advertisements