#include <vector>
#include <string>
#include <iostream>
#include <queue>
using namespace std;
struct TreeNode{
char data;
struct TreeNode *left;
struct TreeNode *right;
}TreeNode;
//先序遍历
void pre_order(TreeNode t){
if (t == NULL)
return;
cout << t->data << " ";
pre_order(t->left);
pre_order(t->right);
}
//中序遍历
void in_order(TreeNode *t){
if (t == NULL)
return;
in_order(t->left);
cout << t->data << " ";
in_order(t->right);
}
//后序遍历
void post_order(TreeNode *t){
if (t == NULL)
return;
post_order(t->left);
post_order(t->right);
cout << t->data << " ";
}
//层序遍历第一种方法
void level_order(TreeNode *t){
queue<Node*> Q;//队列的声明
Node * node ;
Q.push(t);//先压入整棵树的根节点Root
while(!Q.empty())//如果队列不空则一直进行下去
{
node = Q.front();//访问队列的第一个元素
cout << node->data << " ";//输出当前节点的值
if (node->left!=NULL)
{
Q.push(node->left); //如果左节点不为空则压入左节点
}
if (node->right!=NULL)
{
Q.push(node->right); //如果右节点不为空则压入右节点
}
Q.pop(); //弹出当前节点
}
cout<<endl;
}
//层次遍历第二种方法
void level(struct node* root){
int c=0,d=0;
struct node* arr[100];
arr[d++]=root;
while(c<d){
if(arr[c]!=0){//这里的逻辑很有意思
printf("%c",arr[c]->data);
arr[d++]=arr[c]->left;
arr[d++]=arr[c]->right;
}
c++;
}
}
08-26