习题6-3 二叉树重建 UVa536
来源:互联网 发布:python 列表转为元组 编辑:程序博客网 时间:2024/05/16 16:16
1.题目描述:点击打开链接
2.解题思路:一开始的思路是先建立二叉树,再后序遍历,结果不是TLE就是MLE ==,最后省略了建树的过程,直接由先序遍历串,中序遍历串进行后序遍历。
3.代码:
#define _CRT_SECURE_NO_WARNINGS#include<iostream>#include<algorithm>#include<string>#include<sstream>#include<set>#include<vector>#include<stack>#include<map>#include<queue>#include<cassert>#include<cstdlib>#include<cstdio>#include<ctime>#include<cmath>#include<cstring>#include<functional>using namespace std;string pre_order, in_order, post_order;int n;bool solve(const string&pre,const string&in){if (pre.length() == 0) return false;//空串if (pre.length() == 1){ cout << pre; return true;}//只有一个字符时输出int p = in.find(pre[0]);string pre1 = pre.substr(1, p); //直接进行后序遍历string in1 = in.substr(0, p);solve(pre1, in1);pre1 = pre.substr(p + 1, pre.length() - p - 1);in1 = in.substr(p + 1, in.length() - p - 1);solve(pre1, in1);cout << pre[0];}int main(){while (cin>>pre_order>>in_order){post_order.clear();n = pre_order.length();solve(pre_order,in_order);cout << "\n";}return 0;}
0 0
- 习题6-3 二叉树重建 UVa536
- 算法竞赛入门经典 第二版 习题6-3 二叉树重建 Tree Recovery uva536
- 算法竞赛入门经典(第2版)习题6-3 二叉树重建 UVa536
- 二叉树重建(Tree Recovery ,UVa536)
- 树的重建uva536
- 习题6-3 UVa536 Tree Recovery(树的遍历转换)
- 习题6-3 二叉树的重建 UVa 536
- 习题6-3 UVA 536 Tree Recovery 二叉树重建
- 【二叉树】UVA536
- 二叉树习题之重建二叉树
- Uva536 Tree Recovery 【递归建树】【习题6-3】
- UVa-536 习题6-3 二叉树重建(Tree Recovery,ULM 1997)
- 习题6-3 二叉树重建(Tree Recovery, ULM 1997, UVa 536)
- 习题6-11:树重建
- uva536-Tree Recovery-二叉树遍历
- 6+重建二叉树
- 重建二叉树6
- 6、重建二叉树
- linux-多线程
- NSData与CString的无损转化
- android Environment StatFs 类
- coreData 多线程同步
- 网页制作工具哪款软件最好?
- 习题6-3 二叉树重建 UVa536
- 谷歌两年前就在偷偷研制快递无人机
- 远离致癌物
- RailsCasts12 Refactoring User Name Part 3 重构User name(三)
- SQL字符串截取
- 【Trie树】模板题-POJ-2001
- java学习课堂笔记5
- 利用java API实现本地文件上传至hdfs
- 三星推出第六款智能手表:可独立接打电话