UVa-536 习题6-3 二叉树重建(Tree Recovery,ULM 1997)
来源:互联网 发布:班服制作软件 编辑:程序博客网 时间:2024/06/01 08:07
原题链接: UVA-536
题目大意:
输入一棵二叉树的先序遍历和中序遍历序列,输出后序遍历序列。
解题思路:
很基础的一道二叉树的题,和紫书中第六章中的一道例题很相似,边递归建树边输出。后序序列输出时应该将输出放在两个递归函数后面就可以后序序列。不难,不过还有些细节还是需要主要,比如传参的时候左右子树分别在先序和中序的位置需要注意。
代码:
#include<iostream>#include<string>#include<cstring>using namespace std;string preord, inord;void crt_tree(int L1, int R1, int L2, int R2,int n){if (L1 > R1 || L2 > R2)return;char root = preord[L1];int pos=0;while (inord[pos] != root) pos++;int len_left = pos - L2,len_right = R2 - pos;crt_tree(L1 + 1, L1 + len_left, L2, pos - 1, 2 * n);crt_tree(R1 - len_right+1, R1, pos + 1, R2, 2 * n + 1);cout << root;}int main(){while (cin >> preord>> inord){int len = preord.length();crt_tree(0, len - 1, 0, len - 1, 1);cout << endl; }return 0;}
阅读全文
0 0
- UVa-536 习题6-3 二叉树重建(Tree Recovery,ULM 1997)
- 习题6-3 二叉树重建(Tree Recovery, ULM 1997, UVa 536)
- 习题6-3 UVA 536 Tree Recovery 二叉树重建
- uva 536 Tree Recovery(二叉树重建)
- UVA 536 - Tree Recovery(二叉树重建)
- UVA 536 Tree Recovery 二叉树重建(重建二叉树+DFS)
- 算法竞赛入门经典 第二版 习题6-3 二叉树重建 Tree Recovery uva536
- Tree Recovery 字典树 (Ulm Local 1997)
- 习题6-3 二叉树的重建 UVa 536
- 习题6-11 树重建(Tree Reconstruction, UVa 10410)
- poj2255 Tree Recovery 字典树 解题报告(Ulm Local 1997)
- [UVA 536] Tree Recovery 二叉树&dfs
- UVA 536-Tree Recovery(二叉树恢复)
- UVa 536 Tree Recobery 二叉树重建
- (二叉树重建)Tree Recovery(P2255)
- POJ2255,Tree Recovery,二叉树重建
- POJ 2255 Tree Recovery(二叉树重建)
- 二叉树重建(Tree Recovery ,UVa536)
- 关于实现在阿里的Antd的Modal上实现动态表的一种思路
- [leetcode]36. Valid Sudoku@Java解题报告
- 交叉引用造成内存丢失?动手试试吧
- Tomcat内存溢出“permgen space”修改tomcat的配置
- Python终端如何输出彩色字体
- UVa-536 习题6-3 二叉树重建(Tree Recovery,ULM 1997)
- 理解java回调机制
- 一个少女心满满的例子带你入门canvas
- E-R图的画法
- ORACLE 表空间扩展方法
- 语义分割网络FRRN坑点记录
- 人工智能、机器学习和认知计算入门指南
- Nginx--官网中文翻译(中英文对比)--9-nginx怎样处理一个请求How nginx processes a request
- ASP.NET – 数据绑定