#include <iostream>
#include <list>
using namespace std;
//二叉树定义
typedef struct Node{
int data;
Node *lson;
Node *rson;
}Tree, *BeTree;
//二叉树创建
void BuildTree(BeTree &rt){
int data;
cin >> data;
if (data == 0){
rt = NULL;
} else {
rt = new Tree;
rt->data = data;
BuildTree(rt->lson);
BuildTree(rt->rson);
}
}
//获取深度
int GetDeepth(BeTree &rt){
if (rt == NULL){
return 0;
} else {
return max(GetDeepth(rt->lson), GetDeepth(rt->rson)) + 1;
}
}
//先序遍历
void Pre_Order (BeTree &rt){
if (rt != NULL){
cout << rt->data << " ";
Pre_Order(rt->lson);
Pre_Order(rt->rson);
}
}
//中序遍历
void Mid_Order (BeTree &rt){
if (rt != NULL){
Mid_Order(rt->lson);
cout << rt->data << " ";
Mid_Order(rt->rson);
}
}
//后序遍历
void Los_Order (BeTree &rt){
if (rt != NULL){
Los_Order(rt->lson);
Los_Order(rt->rson);
cout << rt->data << " ";
}
}
//层次遍历
void Lel_Order (BeTree &rt){
//使用队列
list<BeTree> t;
if (rt != NULL){
t.push_back(rt);
}
while (t.size() > 0){
cout << t.front()->data << " ";
if (t.front()->lson != NULL){
t.push_back(t.front()->lson);
}
if (t.front()->rson != NULL){
t.push_back(t.front()->rson);
}
t.pop_front();
}
}
int main()
{
BeTree rt;
BuildTree(rt);
cout << "二叉树深度:\n" << GetDeepth(rt) << endl;
cout << "先序遍历:\n" << endl;
Pre_Order(rt);
cout << endl;
cout << "中序遍历:\n" << endl;
Mid_Order(rt);
cout << endl;
cout << "后序遍历:\n" << endl;
Los_Order(rt);
cout << endl;
cout << "层序遍历:\n" << endl;
Lel_Order(rt);
cout << endl;
return 0;
}