这学期学数据结构,觉得写代码好麻烦,不过它让代码的结构改善很多,再难还是要学的..对于二叉树,遍历方法有先序遍历,中序遍历,后续遍历,按层遍历.这里要讲的是按层遍历.有两种方法,第一种是递归,时间效率较低,另一种就是是充分运用了队列的性质,省去了了重复遍历的时间.(偷了个懒,,队列直接用了STL系列函数)
代码如下:
#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;
typedef struct Node{
char data;
struct Node *lchild;
struct Node *rchild;
}Node,*Bitree;
void Create(Bitree &T){
char ch;
cin>>ch;
if(ch!='#'){
T = new Node;
T->data = ch;
Create(T->lchild);
Create(T->rchild);
}
else T=NULL;
}
int Depth(Bitree T){
if(T){
int l=Depth(T->lchild);
int r=Depth(T->rchild);
return