#include<iostream>
using namespace std;
typedef struct BinaryTree
{
char data;
struct BinaryTree *lchild,*rchild;
}BinaryTree,*BiTree;
void CreateBiTree(BiTree &T)
{
char z;
if((z=getchar())==' ')
T=NULL;
else
{
T=(BinaryTree*)malloc(sizeof(BinaryTree));
T->data=z;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
//先序遍历
void preBiTree(BiTree T)
{
if(T!=NULL)
{
cout<<T->data;
preBiTree(T->lchild);
preBiTree(T->rchild);
}
}
//中序遍历
void InBiTree(BiTree T)
{
if(T!=NULL)
{
InBiTree(T->lchild);
cout<<T->data;
InBiTree(T->rchild);
}
}
//后序遍历
void PostBiTree(BiTree T)
{
if(T!=NULL)
{
PostBiTree(T->lchild);
PostBiTree(T->rchild);
cout<<T->data;
}
}
int Depth(BiTree T)
{
if(T==NULL)
{
return 0;
}
else
return 1+(Depth(T->lchild)>Depth(T->rchild)? Depth(T->lchild):Depth(T->rchild));
}
void main()
{
BiTree T;
cout<<"请输入相应
二叉树:"<<endl;
CreateBiTree(T);
cout<<"二叉树的先序遍历为:"<<endl;
preBiTree(T);
cout<<endl;
cout<<"二叉树的中序遍历为:"<<endl;
InBiTree(T);
cout<<endl;
cout<<"二叉树的后序遍历为:"<<endl;
PostBiTree(T);
cout<<endl;
cout<<"二叉树的深度为:"<<endl;
cout<<Depth(T)<<endl;
}
本回答被网友采纳