数据结构和算法经典100题-第27题
来源:互联网 发布:php地区三级联动 编辑:程序博客网 时间:2024/04/30 10:10
已知一棵二叉树的每个节点的值都不同,给定这个二叉树的先序和中序遍历数组,不要重建整棵二叉树,而知通过给定的数组直接生成正确的后序数组。
#include <stdio.h>#include <vector>#include <stack>#include <map>using namespace std;int setPos(int *preArray, int preBeg, int preEnd, int *inArray, int inBeg, int inEnd, int *posArray, int posLen,map<int, int> &inMap) { if (preBeg > preEnd) { return posLen; } int i = inMap[preArray[preBeg]]; posArray[posLen--] = preArray[preBeg]; posLen = setPos(preArray, preEnd-inEnd + i + 1, preEnd, inArray, i + 1, inEnd, posArray, posLen-1, inMap); return setPos(preArray, preBeg + 1, preBeg + i - inBeg, inArray, inBeg, i - 1, posArray, posLen, inMap);}bool getPosArray(int *pre, int *in, int len, int *pos) { if (!pre || !in) { return false; } map<int, int> inMap; for (int i = 0; i < len; ++i) { inMap[in[i]] = i; } setPos(pre, 0, len - 1, in, 0, len - 1, pos, len - 1, inMap); return true;}
路漫漫其修远兮…
0 0
- 数据结构和算法经典100题-第27题
- 数据结构和算法经典100题-第1题
- 数据结构和算法经典100题-第2题
- 数据结构和算法经典100题-第3题
- 数据结构和算法经典100题-第4题
- 数据结构和算法经典100题-第5题
- 数据结构和算法经典100题-第7题
- 数据结构和算法经典100题-第9题
- 数据结构和算法经典100题-第10题
- 数据结构和算法经典100题-第11题
- 数据结构和算法经典100题-第13题
- 数据结构和算法经典100题-第14题
- 数据结构和算法经典100题-第15题
- 数据结构和算法经典100题-第16题
- 数据结构和算法经典100题-第19题
- 数据结构和算法经典100题-第18题
- 数据结构和算法经典100题-第20题
- 数据结构和算法经典100题-第21题
- 深入理解JVM之内存区域与内存溢出 诊断工具
- An internal error occurred during: "Launching FarmeWork on MyEclipse Tomcat"MyEclipse解决方案。
- struts2中的constant详解
- Level of detail
- Android自定义页面指示器
- 数据结构和算法经典100题-第27题
- 刚安装的sql server 无法附加数据库
- Graphics Pipelines
- U盘安装win7sp1,在USB3.0接口上需要驱动
- 链接太长自动换行
- 多线程之线程创建的两种方法(Java)
- Object源码后记
- iOS问题(iOS9 + Xcode7)
- activiti流程设计器activiti designer在eclipse中的安装。