摘要
题目链接 |
难度等级 |
完成状态 |
完成分数 |
最后编辑时间 |
需要注意
|
二叉树的序遍历
|
★☆☆☆☆
|
答案正确
|
100
|
2014-10-11 11:21:21
|
无
|
题意
求二叉树的三序遍历。
题解
就是递归的时候遍历前输出节点、遍历左子树后输出节点、和遍历右子树时输出节点,故称作前序遍历、中序遍历、后序遍历。
代码
3143.cpp代码已折叠
展开折叠内容
|
- #include<iostream>
- int L[20],R[20],n;
- void transversal(const int &node,const int &method)
- {
- if(method==0)
- std::cout<<node<<" ";
- if(L[node])
- transversal(L[node],method);
- if(method==1)
- std::cout<<node<<" ";
- if(R[node])
- transversal(R[node],method);
- if(method==2)
- std::cout<<node<<" ";
- }
- int main()
- {
- std::cin>>n;
- for(int i=1;i<=n;++i)
- std::cin>>L[i]>>R[i];
- for(int i=0;i<3;++i)
- {
- transversal(1,i);
- std::cout<<std::endl;
- }
- return 0;
- }
|