二叉树的先序遍历,中序遍历,后序遍历
#include <iostream>using namespace std;typedef struct BTNode
{char data;struct BTNode* lchild;struct BTNode* rchild;
}BTNode;void preorder(BTNode* p)
{if(p!=NULL){cout<<p->data;preorder(p->lchild);preorder(p->rchild);}
}void inorder(BTNode* p)
{if(p!=NULL){inorder(p->lchild);cout<<p->data;inorder(p->rchild);}
}void postorder(BTNode* p)
{if(p!=NULL){postorder(p->lchild);postorder(p->rchild);cout<<p->data;}
}int main()
{BTNode* rootnode=new BTNode;rootnode->data='A';rootnode->lchild=new BTNode;rootnode->rchild=new BTNode;BTNode* t=rootnode->lchild;t->data='B';t->lchild=new BTNode;t->rchild=NULL;t=t->lchild;t->data='D';t->lchild=NULL;t->rchild=NULL;t=rootnode->rchild;t->data='C';t->lchild=new BTNode;t->rchild=new BTNode;t=rootnode->rchild->lchild;t->data='E';t->lchild=NULL;t->rchild=NULL;t=rootnode->rchild->rchild;t->data='F';t->lchild=NULL;t->rchild=NULL;cout<<"------------preorder---------------"<<endl;preorder(rootnode);cout<<endl;cout<<"------------inorder---------------"<<endl;inorder(rootnode);cout<<endl;cout<<"------------postorder---------------"<<endl;postorder(rootnode);cout<<endl;return 0;
}