# C++ Program to Perform Postorder Recursive Traversal of a Given Binary Tree

C++ProgrammingServer Side Programming

Tree traversal is a form of graph traversal. It involves checking or printing each node in the tree exactly once. The postorder traversal of a binary search tree involves visiting each of the nodes in the tree in the order (Left, Right, Root).

An example of postorder traversal of a binary tree is as follows.

A binary tree is given as follows.

Postorder Traversal is: 1 5 4 8 6

The program to perform post-order recursive traversal is given as follows.

## Example

Live Demo

#include<iostream>
using namespace std;
struct node {
int data;
struct node *left;
struct node *right;
};
struct node *createNode(int val) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = val;
temp->left = temp->right = NULL;
return temp;
}
void postorder(struct node *root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
cout<<root->data<<" ";
}
}
struct node* insertNode(struct node* node, int val) {
if (node == NULL) return createNode(val);
if (val < node->data)
node->left = insertNode(node->left, val);
else if (val > node->data)
node->right = insertNode(node->right, val);
return node;
}
int main() {
struct node *root = NULL;
root = insertNode(root, 4);
insertNode(root, 5);
insertNode(root, 2);
insertNode(root, 9);
insertNode(root, 1);
insertNode(root, 3);
cout<<"Post-Order traversal of the Binary Search Tree is: ";
postorder(root);
return 0;
}

## Output

Post-Order traversal of the Binary Search Tree is: 1 3 2 9 5 4

In the above program, the structure node creates the node of a tree. This structure is a self referential structure as it contains pointers of struct node type. This structure is shown as follows.

struct node {
int data;
struct node *left;
struct node *right;
};

The function createNode() creates a node temp and allocates it memory using malloc. The data value val is stored in data of temp. NULL is stored in left and right pointers of temp. This is demonstrated by the following code snippet.

struct node *createNode(int val) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = val;
temp->left = temp->right = NULL;
return temp;
}

The function postorder() takes the root of the binary tree as argument and prints the elements of the tree in postorder. It is a recursive function. It is demonstrated using the following code.

void postorder(struct node *root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
cout<<root->data<<" ";
}
}

The function insertNode() inserts the required value in the binary tree at its correct position. If the node is NULL, then createNode is called. Otherwise, the correct position for the node is found in the tree. This can be observed in the following code snippet.

struct node* insertNode(struct node* node, int val) {
if (node == NULL) return createNode(val);
if (val < node->data)
node->left = insertNode(node->left, val);
else if (val > node->data)
node->right = insertNode(node->right, val);
return node;
}

In the main() function, the root node is first defined as NULL. Then all the nodes with the required values are inserted into the binary search tree. This is shown below.

struct node *root = NULL;
root = insertNode(root, 4);
insertNode(root, 5);
insertNode(root, 2);
insertNode(root, 9);
insertNode(root, 1);
insertNode(root, 3);

Finally, the function postorder() is called using the root node of the tree and all the tree values are displayed in postorder. This is given below.

cout<<"Post-Order traversal of the Binary Search Tree is: ";
postorder(root);
Published on 05-Oct-2018 12:01:27