p3143 二叉树的序遍历
来源:互联网 发布:总舵主软件 编辑:程序博客网 时间:2024/06/03 18:20
题目描述 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>using namespace std;struct treetype{int l,r;};treetype tree[20];int DLR(int x){cout<<x<<' ';if (tree[x].l!=0)DLR(tree[x].l);if (tree[x].r!=0)DLR(tree[x].r);}int LDR(int x){if (tree[x].l!=0)LDR(tree[x].l);cout<<x<<' ';if (tree[x].r!=0)LDR(tree[x].r);}int LRD(int x){if (tree[x].l!=0)LRD(tree[x].l);if (tree[x].r!=0)LRD(tree[x].r);cout<<x<<' ';}int main(){int n;cin>>n;for (int i=1;i<=n;i++)cin>>tree[i].l>>tree[i].r;DLR(1);cout<<endl;LDR(1);cout<<endl;LRD(1);return 0;}
0 0
- wikioi p3143 二叉树的序遍历
- p3143 二叉树的序遍历
- 二叉树的序遍历
- 二叉树的序遍历
- 二叉树的序遍历
- 二叉树的序遍历
- 二叉树的序遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 电信泄露客户信息,导致客户被骗损失钱财
- 在Activity获取屏幕的宽高
- nginx limit_req限速设置
- 关于iOS多线程,你看我就够了(已更新)
- sql之join用法完全版
- p3143 二叉树的序遍历
- FZU 1752 Quickmod Quickmulti
- 背景图像位置 background-position
- 点我游戏~起点
- Atom飞行手册翻译: 2.14 小结
- 一道IOS面试题
- 分治算法--二分搜索
- hbase单机版初步了解
- 基础总结篇之二:Activity的四种launchMode