一、先序遍历序列创建二叉树
#include <bits/stdc++.h>using namespace std;typedef struct node//二叉树的结构{ char data; struct node *lchild,*rchild;}Node, *Tree;void PreCreateTree(Tree &t)//先序遍历序列创建二叉树{ char ch; cin>>ch; if(ch == '@')//@为特殊字符,表示空 t=NULL; else { t=new node; t->data=ch; PreCreateTree(t->lchild); PreCreateTree(t->rchild); }}int main(){ Tree t=new node; PreCreateTree(t); return 0;}
二、遍历二叉树
1.递归写法:
void PreTraverse(Tree &t){ if(t) { cout<<t->data;//在这输出就是先序遍历 PreTraverse(t->lchild); //放到这里输出就是中序遍历 PreTraverse(t->rchild); //放到这里输出就是后序遍历 }}
2.非递归写法:
#include <bits/stdc++.h>using namespace std;typedef struct node{ char data; struct node *lchild,*rchild;}Node, *Tree;Tree stk[100];//二叉树结构的结构体指针数组void PreCreateTree(Tree &t)//先序遍历序列创建二叉树{ char ch; cin>>ch; if(ch == '@') t=NULL; else { t=new node; t->data=ch; PreCreateTree(t->lchild); PreCreateTree(t->rchild); }}void InoderTraverse(Tree &t)//非递归中序遍历{ int hh=-1,tt=0;//模拟栈 while(t!=NULL || hh>=tt)//当t不为空 或者 栈不为空 { if(t!=NULL)//t不为空就把该结点压入栈,再移到左孩子 { stk[++hh]=t; t=t->lchild; } else//t为空则退栈并输出,再移到右孩子 { t=stk[hh--]; cout<<t->data; t=t->rchild; } } cout<<endl;}int main(){ Tree t=new node; PreCreateTree(t); InoderTraverse(t); return 0;}
3.按层次遍历:
#include <bits/stdc++.h>using namespace std;typedef struct node{ char data; struct node *lchild,*rchild;}Node, *Tree;Tree q[100];void PreCreateTree(Tree &t){ char ch; cin>>ch; if(ch == '@') t=NULL; else { t=new node; t->data=ch; PreCreateTree(t->lchild); PreCreateTree(t->rchild); }}void LayerTraverse(Tree t)//层次遍历{ int hh=0,tt=0;//模拟对列 if(t == NULL) return ; else { q[0]=t;//根结点入队 while(hh<=tt)//当队不为空 { t=q[hh++];//取队首并输出 cout<<t->data; if(t->lchild!=NULL)//有左孩子就把左孩子入队 q[++tt]=t->lchild; if(t->rchild!=NULL)//有右孩子就把右孩子入队 q[++tt]=t->rchild; } }}int main(){ Tree t=new node; PreCreateTree(t); LayerTraverse(t); return 0;}
三、一些操作
1.根据森林的孩子兄弟表示法的先序序列找叶子数量:

#include <bits/stdc++.h>using namespace std;typedef struct node{ char data; struct node *lchild,*rchild;}Node,*Tree;void CreateTree(Tree &t){ char ch; cin>>ch; if(ch == '@') t=NULL; else { t = new node; t->data = ch; CreateTree(t->lchild); CreateTree(t->rchild); }}int TreeLeaf(Tree &t){ if(t == NULL)//根结点为空返回0 return 0; if(t->lchild == NULL)//如果没有左孩子,返回1+右孩子的情况 return 1+TreeLeaf(t->rchild); else//有左孩子,返回左孩子的情况+右孩子的情况 return TreeLeaf(t->lchild)+TreeLeaf(t->rchild);}int main(){ Tree t=new node; CreateTree(t); cout<<TreeLeaf(t); return 0;}
2.找叶子:
int ans=0;int PreTraverse(Tree t){ if(t == NULL) return ans; else { if(t->lchild==NULL&& t->rchild==NULL) ans++; PreTraverse(t->lchild); PreTraverse(t->rchild); } return ans;}
3.求二叉树的深度:
#include <bits/stdc++.h>using namespace std;typedef struct node{ char data; struct node *lchild,*rchild;}Node, *Tree;void PreCreateTree(Tree &t){ char ch; cin>>ch; if(ch == '@') t=NULL; else { t=new node; t->data=ch; PreCreateTree(t->lchild); PreCreateTree(t->rchild); }}int Depth(Tree t){ int n,m;//注意,这里的n和m不能定义在depth函数外!会出错!!! if(t==NULL) return 0; else { m=Depth(t->lchild); n=Depth(t->rchild); return max(n,m)+1; //返回较大的值+1 }}int main(){ Tree t=new node; PreCreateTree(t); cout<<Depth(t)<<endl; return 0;}
4.复制二叉树:
void copy(Tree &t,Tree newt){ if(t == NULL) { newt = NULL; return; } else { newt = new node; newt->data = t->data; copy(t->lchild, newt->lchild); copy(t->rchild, newt->rchild); }}