leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal,剑指offer 6 重建二叉树
不用迭代器的代码
class Solution {
public:
TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
TreeNode* root = NULL;
int length_pre = pre.size();
int length_vin = vin.size();
if(length_pre <= || length_vin <= )
return root;
return ConstructCore(pre,vin,,length_pre-,,length_vin-);
}
TreeNode* ConstructCore(vector<int> pre,vector<int> vin,int start_pre,int end_pre,int start_vin,int end_vin){
int rootval = pre[start_pre];
TreeNode* root = new TreeNode(rootval);
if(start_pre == end_pre || start_vin == end_vin)
return root;
int mid;
for(int i = start_vin;i <= end_vin;i++){
if(pre[start_pre] == vin[i]){
mid = i;
break;
}
}
if(mid > start_vin)
root->left = ConstructCore(pre,vin,start_pre+,mid-start_vin+start_pre,start_vin,mid-);
if(end_vin > mid)
root->right = ConstructCore(pre,vin,mid-start_vin+start_pre+,end_pre,mid+,end_vin);
return root;
}
};
mid是在vin中的索引,与pre相关的只是个数,所以用mid-start_vin来表示有多少个,然后再加上之前的开始坐标
leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal,剑指offer 6 重建二叉树的更多相关文章
- 105.Construct Binary Tree from Preorder and Inorder Traversal---《剑指offer》面试6
题目链接 题目大意:根据先序遍历和中序遍历构造二叉树. 法一:DFS.根据模拟步骤,直接从先序和中序数组中找值然后加入二叉树中,即先从先序数组中确定根结点,然后再去中序数组中确定左子树和右子树的长度, ...
- [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- (二叉树 递归) leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal (用先序和中序树遍历来建立二叉树)
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal ----- java
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- Java for LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...
- [leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal (Medium)
原题 题意: 根据先序和中序得到二叉树(假设无重复数字) 思路: 先手写一次转换过程,得到思路. 即从先序中遍历每个元素,(创建一个全局索引,指向当前遍历到的元素)在中序中找到该元素作为当前的root ...
- Leetcode#105 Construct Binary Tree from Preorder and Inorder Traversal
原题地址 基本二叉树操作. O[ ][ ] [ ]O[ ] 代码: TreeNode *restore(vector< ...
随机推荐
- 【问题总结】万万没想到,竟然栽在了List手里
说明 昨天同事开发的时候遇到了一个奇怪的问题. 使用Guava做缓存,往里面存一个List,为了方便描述,称它为列表A,在另一个地方取出来,再跟列表B中的元素进行差集处理,简单来说,就像是下面这样: ...
- 虚拟机vmware11装Mac ox 10.8 解决windows8.1下unlocker202 vmxsmc.exe已停止工作的问题
转载:http://tieba.baidu.com/p/3485956366 先说说故事吧,相信很多图钉都用过VMware Workstation这个软件.我系统是win8.1up1,之前用VMwar ...
- 《深度学习-改善深层神经网络》-第二周-优化算法-Andrew Ng
目录 1. Mini-batch gradient descent 1.1 算法原理 1.2 进一步理解Mini-batch gradient descent 1.3 TensorFlow中的梯度下降 ...
- python-根据URL地址下载文件
博主个人网站:https://chenzhen.online 使用Python中提供的urllib.request下载网上的文件 #coding=utf-8 """ 目标 ...
- Tyvj2017清北冬令营入学测试
P4744 A's problem(a) 时间: 1000ms / 空间: 655360KiB / Java类名: Main 背景 冬令营入学测试题,每三天结算一次成绩.参与享优惠 描述 这是一道有背 ...
- 洛谷P3455 [POI2007]ZAP-Queries(莫比乌斯反演)
传送门 设$$f(k)=\sum_{i=1}^{a}\sum_{j=1}^{b}[gcd(i,j)=k]$$ $$g(n)=\sum_{n|k}f(k)=\lfloor\frac{a}{n}\rflo ...
- IT兄弟连 JavaWeb教程 监听器1
1 基本概念 监听器是一个专门用于对其他对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时,立即采取相应的行动.监听器其实就是一个实现特定接口的普通java程序,这个程序 ...
- Spark系列视频
大数据生态圈很大,很多开发者都仅仅接触到某个单一产品. Spark 是近年来比较流行的大数据计算框架,系统.平台要想用好Spark 这个产品,需要用到很多的产品. 本视频系列主要是为准备入坑大数据的童 ...
- Exception inside CORBA when accessing a remote bean
http://stackoverflow.com/questions/23291520/exception-inside-corba-when-accessing-a-remote-bean
- background-attachment:fixed不兼容性
ios系统和某些移动端background-attachment:fixed不兼容性,没有任何效果,但可以hack一下就可以了,代码如下: ps:想在哪个标签加背景,可以在它class后:before ...