数据结构OJ作业 二叉树
2017-04-22 23:03
162 查看
题目传送门:http://poj.org/problem?id=2255
Tree Recovery
给出一个二叉树的前序和中序,求二叉树的后序。
节约空间,并不实际建树,而是一边搜索一边输出。
同hdoj1710,写完了这篇blog才发现以前也写过,尴尬……
http://blog.csdn.net/jlu_nnbs/article/details/55806227
Tree Recovery
给出一个二叉树的前序和中序,求二叉树的后序。
节约空间,并不实际建树,而是一边搜索一边输出。
同hdoj1710,写完了这篇blog才发现以前也写过,尴尬……
http://blog.csdn.net/jlu_nnbs/article/details/55806227
#include <cstdio> #include <cstring> using namespace std; char pre[30],in[30]; void build(int l, int r, int root) { if (l > r) return ; int loc; for (int i = l; i <= r; i ++) { if (pre[root] == in[i]) { loc = i; break; } } // 中序左子树根的位置就是当前位置加一 build(l, loc - 1, root + 1); // 而右子树的根是所有左子树结点遍历完后的第一个 build(loc + 1, r, root + loc - l + 1); printf("%c",in[loc]); return ; } int main() { while (~scanf(" %s %s",pre,in)) { int len = strlen(pre); build(0,len - 1,0); printf("\n"); } return 0; }
相关文章推荐
- 数据结构作业——brothers(二叉树)
- 数据结构OJ作业——队列
- 数据结构作业:利用中序遍历和后序遍历构建二叉树(RMQ转LCA)
- 【数据结构作业四】以二叉链表作存储结构,建立一棵二叉树,并输出该二叉树的先序、中序、后序遍历序列、高度和其叶子结点数。
- 数据结构OJ作业——栈
- 数据结构作业——brothers(二叉树)
- 截获数据分析--第一次作业
- 学习引擎作业参考资源:数据挖掘技术
- 第一次作业:网络数据截获
- 数据结构(二叉树)C#描述
- 数据结构之二叉树
- 截获一段数据(计算机网络技术作业)
- 数据结构(C++)--二叉树
- 第一次作业:网络数据截获
- 数据库通过作业维护数据
- 数据结构与算法(C#实现)系列---二叉树
- 第二次数据结构作业
- 数据结构(严蔚敏)二叉树的二叉链表存储表示
- Ethereal数据截获作业
- 数据结构与算法(C#实现)系列---二叉树