HDU 1710 二叉树水题
来源:互联网 发布:cn是哪个国家的域名 编辑:程序博客网 时间:2024/05/19 06:47
传送门 : HDU 1710
题解
水递归, 记得以前刚学数据结构的时候还用结构体模拟字符串做
用字符串是通过string的find函数, 这里读取的时候可以直接记录先序遍历的元素在中序中的位置
输出左右子树, 然后输出根
AC code: (78MS, 略慢)
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 1000 + 5;int pre[maxn], in[maxn], n, pos[maxn];void creat(int l, int r, int L, int R) { if (L == R) { printf("%d ", in[L]); return; } int id = pos[pre[l]]; if(id > L) creat(l + 1, l + id - L, L, id - 1);//边界, 左 if(id < R) creat(l + 1 + id - L, r, id + 1, R);//右 printf("%d%s", in[id], l == 1 ? "\n" : " ");}int main() {#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin);#endif while (~scanf("%d", &n)) { for (int i = 1; i <= n; ++i) scanf("%d", &pre[i]); for (int i = 1; i <= n; ++i) { scanf("%d", &in[i]); pos[in[i]] = i; } creat(1, n, 1, n); } return 0;}
0 0
- HDU 1710 二叉树水题
- hdu 1710 二叉树遍历
- hdu 1710 二叉树重建
- hdu 1710恢复二叉树
- HDU 1710 二叉树恢复与遍历
- hdu 1710二叉树的遍历
- HDU 1710 二叉树的遍历
- HDU 1710 Binary Tree Traversals 二叉树
- HDU 1710 Binary Tree Traversals(二叉树)
- hdu 1305 二叉树
- hdu-二叉搜索树
- 二叉堆 hdu 4006
- HDU 5444 二叉树
- 二叉搜索树 HDU
- HDU 1671 Phone List 二叉树水题 数组建树法
- hdu 1710 binary tree traversals 二叉树遍历
- 二叉树 遍历 hdu-1710-Binary Tree Traversals
- HDU-1710 Binary Tree Traversals 二叉树遍历
- 第十五周项目二字符串分段
- android sudio 如何获取sha1与md5值
- Eclipse代码格式化无效解决方案
- Java提高篇----详解内部类
- POJ 1463 Strategic game
- HDU 1710 二叉树水题
- GCC或G++编译器使用命令
- Hybrid应用,WEB部分的国际化
- 费诺编码
- 第十六周项目1-(5)归并排序
- CSDN维护了,还好空间还能继续使用
- 创建类型数据
- 内存溢出
- CS231n课程笔记翻译