postorder traversal from inorder and preorder code example
Example 1: inorder preorder postorder
preorder: parent => left => right
inorder: left => parent => right
postorder: left => right => parent
Example 2: preorder traversal
#include <iostream>
using namespace std;
class node{
public:
int data;
node* left;
node* right;
node(int d){
data = d;
left = NULL;
right = NULL;
}
};
node* buildTree(){
int d;
cin>>d;
if(d==-1){
return NULL;
}
node * root = new node(d);
root->left = buildTree();
root->right = buildTree();
return root;
}
void printIn(node*root){
if(root==NULL){
return;
}
printIn(root->left);
cout<<root->data<<" ";
printIn(root->right);
}
int main(){
node* root = buildTree();
printIn(root);
return 0;
}