二叉树的遍历
2013-04-12 09:14
127 查看
# include<iostream> using namespace std; struct Tree { char chr; Tree *l,*r; }; Tree *root; int sign,Count; void Init(struct Tree *&t)// { char a; if(t==NULL) { t=new Tree; cin>>a; t->chr=a; t->l=NULL; t->r=NULL; if(a=='0') { sign++; return; } else { if(sign>=2)Count++;//后面有两个0的节点,一定就是叶子 sign=0; } } Init(t->l);//初始化左子树 Init(t->r);//初始化右子树 } void FirstSequence(struct Tree *t)//先序 { if(t->chr=='0')return; cout<<t->chr<<" "; FirstSequence(t->l); FirstSequence(t->r); } void InSequence(struct Tree *t)//中序 { if(t->chr=='0')return; InSequence(t->l); cout<<t->chr<<" "; InSequence(t->r); } void AfterSequence(struct Tree *t)//后序 { if(t->chr=='0')return; AfterSequence(t->l); AfterSequence(t->r); cout<<t->chr<<" "; } int main() { Count=0; sign=0; Init(root); FirstSequence(root); cout<<endl; InSequence(root); cout<<endl; AfterSequence(root); cout<<endl; if(sign>=2)Count++;//如果最后输入两个0,不会被判断,在此完善 cout<<Count<<endl; return 0; }
相关文章推荐
- 二叉树的遍历
- 二叉树的建立,以及非递归遍历
- 二叉树的遍历(C++、STL)
- 按行输出,之字形输出,翻转二叉树----层次遍历的应用
- 二叉树的遍历
- [C语言]二叉树(建树、是否相等、三种遍历)
- 非递归遍历二叉树
- 二叉树的循环遍历方法,Java实现。利用栈和arraylist
- 数据库 SQL 遍历父子关系表(二叉树)获得所有子节点 所有父节点
- Morris方法遍历二叉树
- 二叉树前序,中序,后序的遍历【递归(借用栈实现)和非递归】
- 二叉树的建立和遍历
- 二叉树系列之二叉树的遍历
- [置顶] 二叉树的遍历
- 中序二叉树、队列实现层次遍历、中序线索二叉树的创建与遍历
- 数据结构----二叉树的遍历
- 层次遍历二叉树
- 从上往下打印二叉树(分层遍历)
- 二叉树的层次遍历(BFS),二叉树的所有路径,二叉树的最大路径和(分治)
- 数据结构实验之二叉树二:遍历二叉树