UVA 536 Tree Recovery (二叉树的遍历)
来源:互联网 发布:淘宝网话费充值代理 编辑:程序博客网 时间:2024/04/29 13:07
题意
给出 一个二叉树的前序和中序遍历,输出 后序遍历。
分析
没啥好分析, 看紫书吧。。。
每一个前序子树的第一都是根。。。
1A 还是比较激动的。
代码
#include <bits/stdc++.h>using namespace std;int a[30],b[30];int n, m;int ltree[30],rtree[30];string s1, s2;int build_tree(int l, int r, int ll, int rr){ if(ll > rr) return 0; int root = a[l]; int p = ll; while(b[p] != root) p++; int cnt = p - ll; ltree[root] = build_tree(l+1,l+cnt,ll,p-1); rtree[root] = build_tree(l+cnt+1,r,p+1,rr); return root;}void dfs(int root){ if(root == 0) return; dfs(ltree[root]); dfs(rtree[root]); char d = 'A'+(root-1); cout << d;}int main(){ //freopen("in.txt","r",stdin); while(cin >> s1 >> s2) { n = s1.size(); m = s2.size(); for(int i = 0; i < n; i++) a[i] = s1[i] - 'A' + 1; for(int i = 0; i < m; i++) b[i] = s2[i] - 'A' + 1; int r = build_tree(0,n-1,0,m-1); dfs(r); cout << endl; } return 0;}
0 0
- UVA 536 Tree Recovery (二叉树的遍历)
- 536 - Tree Recovery(二叉树遍历)
- UVa 536 Tree Recovery——树的遍历
- uva 536 Tree Recovery(二叉树重建)
- [UVA 536] Tree Recovery 二叉树&dfs
- POJ 2255 Tree Recovery 二叉树的遍历
- poj 2255 Tree Recovery(二叉树的遍历)
- poj 2255 Tree Recovery (二叉树的顺序遍历)
- UVA 536-Tree Recovery(二叉树恢复)
- 习题6-3 UVA 536 Tree Recovery 二叉树重建
- UVA 536 - Tree Recovery(二叉树重建)
- POJ 2255 Tree Recovery 二叉树遍历
- poj2255 Tree Recovery 二叉树遍历
- POJ2255 Tree Recovery 二叉树遍历
- POJ 2255 Tree Recovery (二叉树遍历)
- uva536-Tree Recovery-二叉树遍历
- POJ2255 Tree Recovery(二叉树遍历)
- UVA 536 - Tree Recovery(树)
- Leetcode 276. Paint Fence (经典DP)
- Atitit 解决Unhandled event loop exception错误的办法
- Palindrome Number
- Json解析数组对象
- Dijkstra最短路径算法的优化
- UVA 536 Tree Recovery (二叉树的遍历)
- Nexus更新索引(http://www.cnblogs.com/meet/p/5060892.html)
- Atitit.gui api自动化调用技术原理与实践
- 为什么这么多人在泰国买房
- 常量指针、指针常量
- 电子线路与电磁干扰的分析
- WebSocket对象的“readyState”属性记录连接过程中的状态值
- feature_names mismatch XGBoost错误解析
- Android官方培训课程1.3(语言、屏幕、系统)适配