codevs3143二叉树的序遍历
来源:互联网 发布:foreach遍历二维数组 编辑:程序博客网 时间:2024/06/06 16:55
题目描述 Description
求一棵二叉树的前序遍历,中序遍历和后序遍历
输入描述 Input Description
第一行一个整数n,表示这棵树的节点个数。
接下来n行每行2个整数L和R。第i行的两个整数Li和Ri代表编号为i的节点的左儿子编号和右儿子编号。
输出描述 Output Description
输出一共三行,分别为前序遍历,中序遍历和后序遍历。编号之间用空格隔开。
样例输入 Sample Input
5
2 3
4 5
0 0
0 0
0 0
样例输出 Sample Output
1 2 4 5 3
4 2 5 1 3
4 5 2 3 1
数据范围及提示 Data Size & Hint
n <= 16
就当是复习一下二叉树了吧,三种遍历方式不是很难
#include <iostream>#include<algorithm>using namespace std;int a[17][3];void pre(int n ){if (n==0) return;cout << n<<" ";pre(a[n][1]);pre(a[n][2]);}void mid(int n){if (n == 0)return;mid(a[n][1]);cout << n<<" ";mid(a[n][2]);}void post(int n){if (n == 0)return;post(a[n][1]);post(a[n][2]);cout << n<<" ";}int num[102];int n;int main(){int n;cin >> n;for (int i = 1; i <= n; i++)cin >> a[i][1] >> a[i][2];pre(1);cout << endl;mid(1);cout << endl;post(1);cout << endl;//cin >> n;return 0;}
阅读全文
0 0
- [codevs3143] 二叉树的序遍历
- codevs3143二叉树的序遍历
- codevs3143 二叉树的遍历
- 二叉树的序遍历
- 二叉树的序遍历
- 二叉树的序遍历
- 二叉树的序遍历
- 二叉树的序遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 集群计算
- 子div 的margin-top不生效
- 旅游租赁蓝海市场,平衡车租赁前景探讨
- 云主机Linux系统部署Apache Web服务
- Leetcode [8. String to Integer (atoi)]
- codevs3143二叉树的序遍历
- Layout POJ
- 史上最优秀的,最快的编辑器VIM(上古神器)
- 解决shadowsocks2.8.2报错问题
- Eclipse中设置作者日期等信息
- 用c语言实现的猜数字小游戏
- 4888. 【NOIP2016提高A组集训第14场11.12】最近公共祖先 (2017.10B组)
- CNN卷积神经网络中的AlexNet、VGG、GoogLeNet、ResNet对比
- OSG+QGIS在QT Creator下环境配置