不用迭代器的代码

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. pure css做的手机页面

    <!doctype html> <html> <head> <meta http-equiv="Content-type" content ...

  2. lower_bound和upper_bound使用说明

    #include <bits/stdc++.h> using namespace std; int main() { ]; ;i<=;i++) { a[i] = i*; } ;i&l ...

  3. VMware Workstation “无法连接 MKS: 套接字连接尝试次数太多;正在放弃。” 解决方法【转】

    今天和往常一样打开电脑,打开VMware Workstation,打开其中的一台虚拟机,以前都是这么打开没有问题,今天打开虚拟机突然提示“无法连接 MKS: 套接字连接尝试次数太多:正在放弃.”. 经 ...

  4. 在Visual Studio中编译Linux的一些问题

    相对路径: 在windows下,和当前文件同一个目录下的引用会这么写: #include "SubDirectory\header.h" 或者 #include "Sub ...

  5. IT兄弟连 JavaWeb教程 过滤器与监听器经典面试题

    1.谈谈你对Servlet过滤器的理解 过滤器是Servlet2.3规范中定义的一种小型的.可插入的Web组件.用来拦截Servlet容器的请求和响应过程,以便查看.提取客户端和服务器之间正在交换的数 ...

  6. SpringBoot2.0 基础案例(06):引入JdbcTemplate,和多数据源配置

    一.JdbcTemplate对象 1.JdbcTemplate简介 在Spring Boot2.0框架下配置数据源和通过JdbcTemplate访问数据库的案例. SpringBoot对数据库的操作在 ...

  7. react-native-contact 安卓已测试,

    1. 下载模块    npm install react-native-contacts --save 2.安卓配置: a.在android/settings.gradle include ':rea ...

  8. CC14:集合栈

    题目 请实现一种数据结构SetOfStacks,由多个栈组成,其中每个栈的大小为size,当前一个栈填满时,新建一个栈.该数据结构应支持与普通栈相同的push和pop操作. 给定一个操作序列int[] ...

  9. LVM逻辑卷基本概念以及相关操作

    一.LVM概念 LVM(Logical Vloume Manager):它是linux环境下对磁盘进行管理的一种机制,正常挂载的磁盘在磁盘资源快要耗尽时,无法动态拉伸增加资源,或由于特殊情况需要动态缩 ...

  10. ICPC-无限路之城(数学+思维)

    链接:https://ac.nowcoder.com/acm/contest/321/C 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言2621 ...