剑指offer重构二叉树 给出二叉树的前序和后序重构二叉树
题目描述
class Solution {
public:
struct TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> in) {
if (pre.empty() || in.empty()) return NULL;
TreeNode *root = NULL;
root = Constructcore(pre, in, , (int)pre.size()-, , (int)in.size()-);
return root;
}
TreeNode *Constructcore(vector<int> pre, vector<int> in,
int startPre, int endPre,
int startIn, int endIn ){
int rootValue = pre[startPre];
TreeNode *root = new TreeNode(rootValue);
if (startPre == endPre){
if (startIn == endIn &&pre[startPre] == in[startIn])
return root;
}
int pos;
for (pos = startIn; pos <= endIn; pos++){
if (pre[startPre] == in[pos]) break;
}
int leftlength = pos - startIn;
int rightlength = endIn - pos;
if (leftlength > ){
root->left = Constructcore(pre, in, startPre + ,startPre + +leftlength,
startIn, pos -
);
}
if (rightlength > ){
root->right = Constructcore(pre, in, startPre + + leftlength, endPre,
pos + , endIn
);
}
return root;
}
};
剑指offer重构二叉树 给出二叉树的前序和后序重构二叉树的更多相关文章
- 《剑指offer》面试题24 二叉搜索树的后序遍历序列 Java版
(判断一个元素均不相同的序列是否为一个BST的LRD) 书中方法:首先对于二叉搜索树,左子树中的所有元素小于根节点小于右子树中的所有元素,然后后序遍历序列最后一个元素是根节点,这是我们已知的条件.这道 ...
- 剑指offer面试题24:二叉搜索树的后序遍历序列
题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是返回true,否则返回false. 假设输入的数组任意两个数字都不相同 解题思路:二叉搜索树的特点是根节点的左子树的值小于等 ...
- 《剑指offer》面试题33. 二叉搜索树的后序遍历序列
问题描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果.如果是则返回 true,否则返回 false.假设输入的数组的任意两个数字都互不相同. 参考以下这颗二叉搜索树: 5 / \ ...
- 剑指Offer面试题:22.二叉搜索树的后序遍历序列
一.题目:二叉搜索树的后序遍历序列 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true,否则返回false.假设输入的数组的任意两个数字都互不相同. 例如在下面 ...
- 剑指Offer(二十二):从上往下打印二叉树
剑指Offer(二十二):从上往下打印二叉树 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/b ...
- 【剑指offer】堆栈推弹出序列
转载请注明出处:http://blog.csdn.net/ns_code/article/details/26077863 剑指offer上的第22题,九度OJ上AC. 题目描写叙述: 输入两个整数序 ...
- 剑指offer——面试题32.1:分行从上到下打印二叉树
void BFSLayer(BinaryTreeNode* pRoot) { if(pRoot==nullptr) return; queue<BinaryTreeNode*> pNode ...
- 剑指offer:1.找出数组中重复的数(java版)
数组中重复的数:题目:找出数组中重复的数,题目描述:在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任 ...
- 剑指 offer set 20 打印出和为 s 的连续正序序列
题目 100 可以由 9~16, 或者 18 ~ 22 组成 思路 1. 与 Leetcode Container With Most Water 有些类似, 依然是平移题目. 但这道更加复杂 2. ...
随机推荐
- #13:人十我一Orz——6
水题放送,写得依旧丑: #include <cstdio> #include <cstring> #include <cmath> #include <alg ...
- Technocup 2017 - Elimination Round 1 (Unofficially Open for Everyone, Rated for Div. 2) C
This is an interactive problem. You should use flush operation after each printed line. For example, ...
- 模拟IO 读写压力测试
#### 本实验室通过创建一个测试表myTestTable ,分配在一个足够大小的表空间. ###然后通过 insert select 方式,创建100个后台进程进行读写操作,每个后台进程预计时间20 ...
- Linux上的常用命令(含在Linux上部署springboot工程所要用到的命令)
日常使用命令: 开关机:(shutdown命令的时间参数似乎是一定要的) shutdown -h now ——h的意思就是服务停掉之后立即关系,shutdown命令的时间参数是必须要的,now相当于是 ...
- corn表达式 经典
https://www.cnblogs.com/GarfieldTom/p/3746290.html
- 遍历list集合的三种方式
List<String> list1 = new ArrayList<String>(); list1.add("1"); list1.add(" ...
- yii2 使用gii生成代码文件
访问地址: http://localhost/yii2-test/web/index.php?r=gii 如果你通过本机以外的机器访问 Gii,请求会被出于安全原因拒绝. 在web.php修改gii ...
- spring @RequestBody 和 @RequestParams 同时使用
@RequestBody 和 @RequestParams 是可以同时使用的. @RequestBody 接受的数据类型是 content-type:"application/json&qu ...
- OutOfMemory
查看图片格式,如果为PNG,可更改为jpg.图片会变小. 停止activity 当activity调用onStop()方法, activity不再可见,并且应该释放那些不再需要的所有资源.一旦acti ...
- mysql命令行执行时不输出列名(字段名)
-N 即可 如:mysql -N -e "select * from test" 摘自:http://blog.csdn.net/eroswang/article/details/ ...