不用迭代器的代码

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. Halcon - 数据类型

    这里给大家分享 Halcon 的数据类型结构图,方便初学者认知 Halcon 图1 Halcon Datatype

  2. app.config 配置的一种用法

    app.config文件 <?xml version="1.0" encoding="utf-8" ?> <configuration> ...

  3. 51nod1113(矩阵快速幂模板)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1113 题意:中文题诶- 思路:矩阵快速幂模板 代码: #inc ...

  4. 前端js模糊搜索(模糊查询)

    1.html结构: <label for="searchShop" class="clear pos-a" style="top:17px;&q ...

  5. django视图 CBV 和 FBV

    目录 视图 CBV 和 FBV 什么是视图? FBV function based view 基于函数的视图 CBV class based view 基于类的视图 小技巧 CBV 如何获取页面请求类 ...

  6. Spring包的依赖关系以及提供下载

    https://www.jianshu.com/p/5b0c96975164 这篇简书叙述的很完整 一下是个人整和的炸包,里面有很全面的Spring包, 还有一些其他包,都是官网下载 emmmm... ...

  7. 跟踪记录ABAP对外部系统的RFC通信

    对SAP系统而言,RFC最常见的系统间通信方式,SAP与SAP系统及SAP与非SAP系统之间的连接都可以使用它.它的使用便利,功能强大,在各种接口技术中,往往是最受(ABAP开发者)青睐的选择. 查询 ...

  8. redids

    Redis 地理位置(geo) Redis 键(key) Redis 字符串(String) Redis 哈希(Hash) Redis 列表(List) Redis 集合(Set) Redis 有序集 ...

  9. Educational Codeforces Round 65 (Rated for Div. 2) A. Telephone Number

    链接:https://codeforces.com/contest/1167/problem/A 题意: A telephone number is a sequence of exactly 11  ...

  10. MySQL数据库(3)

    外键的变种(三种关系),数据的增删改,单表查询,多表查询 一.外键的变种(三种关系) 本节重点: 如何找出两张表之间的关系 表的三种关系 一.介绍 因为有foreign key的约束,使得两张表形成了 ...