UVa 548 Tree 根据后序遍历和中序遍历建树后DFS
来源:互联网 发布:万得股票软件mac版本 编辑:程序博客网 时间:2024/05/22 01:41
#include <cstdio>#include <cstring>#include <iostream>#include <cstdlib>#include <sstream>#include <algorithm>#define PAUSE system("pause")using namespace std;const int maxv = 10000 + 10;//lch和rch分别记录当前节点的左右子树的根节点int in_order[maxv], post_order[maxv], lch[maxv], rch[maxv];int n, ans, pos;bool read(int *a) {string line;if (!getline(cin, line)) return false;stringstream ss(line);int x;n = 0;while (ss >> x) a[n++] = x; //流化输入return true;}//inorder [L1, R1], postorder [L2, R2]int build(int L1, int R1, int L2, int R2) {if (L1 > R1) return 0; //空树返回0int root = post_order[R2];int p = L1;while (in_order[p] != root) p++; //debugint cnt = p - L1; //左子树长度lch[root] = build(L1, L1 + cnt - 1, L2, L2 + cnt - 1);rch[root] = build(p + 1, R1, L2 + cnt, R2 - 1);return root;}void dfs(int root, int sum) {sum += root;if (!lch[root] && !rch[root]) {if (sum < ans || (sum == ans && root < pos)) {ans = sum;pos = root;}return;}if (lch[root]) dfs(lch[root], sum); //debug if(lch[root])if (rch[root]) dfs(rch[root], sum); //debug if(rch[root])}int main(){while (read(in_order)) {read(post_order);int root = build(0, n - 1, 0, n - 1);ans = pos = 0x7fffffff;dfs(root, 0);printf("%d\n", pos);}return 0;}
0 0
- UVa 548 Tree 根据后序遍历和中序遍历建树后DFS
- UVa 548 Tree (中序遍历&后序遍历&DFS)
- uva 548 Tree (根据中序遍历和后序遍历求先序遍历)
- 例题6-8 树 Tree UVa 548 中序遍历+后序遍历建树
- 二叉树 根据前序遍历和中序遍历 或者 后序遍历和中序遍历建树
- 模板-根据中序,后序建树层次遍历输出
- 由后序遍历和中序遍历建树
- uva 548 Tree(通过后序,先序重建树+dfs)
- 二叉树——根据先序(后序)和中序遍历建树
- 根据后序遍历和中序遍历求先序遍历
- 根据前序与中序遍历建树后以后序遍历输出
- 根据后序与中序遍历建树层序遍历输出
- 顺序建树后序遍历
- UVA表达式树的后序遍历和层次遍历和建树
- EOJ1811 根据层号表示建树并后序遍历
- 根据后序和中序建树
- Tree Recovery(二叉树建树,后序遍历)
- POJ 2255Tree Recovery 二叉树重建(根据前序遍历和中序遍历写出后序遍历)
- java keylistener 问题
- UIUC同学Jia-Bin Huang收集的计算机视觉代码合集
- c++第四次上机作业
- MFC制作简易音乐播放器
- 关于rs.isAfterLast和rs.next的用法
- UVa 548 Tree 根据后序遍历和中序遍历建树后DFS
- Vim学习指南(建议通过前3个层次)
- 机器学习算法
- 练习三1003
- 有趣的vim练习游戏
- 一条Log引发的血案
- ExpandableListView修改group图标
- preference-->java-->code style-->code templates
- zend studio 导入php项目到工作空间