C program for postorder traversal in binary tree.

Posted by Mangesh on August 11, 2018

Program in c for postorder traversals of binary tree.

Description:

In postorder traversal a binary trees, we first traversal to the children of a node and then we come to the root(or parent) of the nodes. The sequence in which we traversal for postorder is left node, right node, then the parent node.

Program :

#include<stdio.h>
#include<conio.h>
typedef struct node
{
  int data;
  struct node *left;
  struct node *right;
} node;

node *create()
{
    node *p;
    int x;
    printf("Enter data(-1 for no node):");
    scanf("%d",&x);

    if(x==-1)
	return NULL;

    p=(node*)malloc(sizeof(node));
    p->data=x;
    printf("Enter left child of %d:\n",x);
    p->left=create();
    printf("Enter right child of %d:\n",x);
    p->right=create();
    return p;
}

void postorder(node *t)
{
  if(t!=NULL)
  {
    postorder(t->left);
    postorder(t->right);
    printf("  %d",t->data);
  }
}
void main()
{
  node *root;
  clrscr();
  root=create();

  printf("\nThe postorder traversal of tree is: ");
  postorder(root);
  getch();
}

Output:

postorder traversal on binary tree executed and run in turbo c++ 3.2

Written with from Mangesh.

Related Post
1 C program to create a binary tree.
2 C program for inorder traversal in binary tree.
3 C program for preorder traversal in binary tree.
4 C program for postorder traversal in binary tree.
Latest Post
1 C program to implement Queue using linked list.
2 C program for binary search tree (BST).
3 C program to search an element in linked list.
4 C program for postorder traversal in binary tree.
5 C program for preorder traversal in binary tree.