不用迭代器的代码

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 重建二叉树的更多相关文章

  1. 105.Construct Binary Tree from Preorder and Inorder Traversal---《剑指offer》面试6

    题目链接 题目大意:根据先序遍历和中序遍历构造二叉树. 法一:DFS.根据模拟步骤,直接从先序和中序数组中找值然后加入二叉树中,即先从先序数组中确定根结点,然后再去中序数组中确定左子树和右子树的长度, ...

  2. [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 ...

  3. (二叉树 递归) 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. [leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal (Medium)

    原题 题意: 根据先序和中序得到二叉树(假设无重复数字) 思路: 先手写一次转换过程,得到思路. 即从先序中遍历每个元素,(创建一个全局索引,指向当前遍历到的元素)在中序中找到该元素作为当前的root ...

  9. Leetcode#105 Construct Binary Tree from Preorder and Inorder Traversal

    原题地址 基本二叉树操作. O[       ][              ] [       ]O[              ] 代码: TreeNode *restore(vector< ...

随机推荐

  1. redis win连接以及配置连接密码

    redis连接格式为 redis-cli -h host -p port -a password 但由于刚安装的redis是没有密码的 因此可以进行直接连接, cd转到redis目录里 redis-c ...

  2. ue4 碰撞检测测试

    记录几条物理相关 测试条件,1使用setActorLocation移动,3使用控制器的移动 1 moveCube  2 targetCube  3 Character 两个Cube的碰撞事件 1和2的 ...

  3. uoj#279. 【UTR #2】题目交流通道(容斥+数数)

    传送门 先考虑无解的情况,为以下几种:\(dis_{i,j}+dis_{j,k}<dis_{i,k}\),\(dis_{i,i}\neq 0\),\(dis_{i,j}\neq dis_{j,i ...

  4. 下拉刷新,上拉加载功能--dropload.js的使用

    这段时间工作太忙了,没时间更新博客内容,在这段时间,也学习到了不少新的知识.今天先整理一下dropload.js的使用方法吧,这个是在为项目中使用过的插件,很好用,但是真正用到项目中还是会有一些小小的 ...

  5. How to install your SSL Certificate to your Windows Server

    Installation: Open the ZIP file containing your certificate. Save the file named your_domain_name.ce ...

  6. SonarQube总结

    官网:https://www.sonarqube.org/ 一款代码质量管理开源平台.

  7. $(function(){})返回值$(document)

    $(function(){})返回值为$(document). 做出下面的操作:可以使得页面背景改变. $(function (){}).find('body').css('background',' ...

  8. CodeForces - 894A-QAQ(思维)

    "QAQ" is a word to denote an expression of crying. Imagine "Q" as eyes with tear ...

  9. 基于TCP协议网络编程

    1.TCP/IP是一种可靠的网络协议,它在通信的两端各建立一个Socket,从而在通信的两端之间形成网络虚拟链路: 一旦建立了虚拟的网络链路,两端的程序就可以通过虚拟链路来进行通信: 2.Java对基 ...

  10. 修改Tomcat和Jetty默认JDK

    tomcat: sed -i 's/java-7-oracle/java-8-oracle/g' /etc/init.d/tomcat7 Jetty echo 'JAVA_HOME=/usr/lib/ ...