/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if (preorder.empty())
return NULL;
int root_index = ;
return createBST(preorder,inorder,root_index,preorder.size()-,root_index);
}
TreeNode* createBST(vector<int>& preorder, vector<int>& inorder, int start, int end, int& index) {
int v = preorder[index];
int i = start;
for (i; i <= end; i++)
if (v == inorder[i])
break;
TreeNode* root = new TreeNode(v);
if (i- >= start)
root->left = createBST(preorder,inorder,start,i-,++index);
if (end >= i+)
root->right = createBST(preorder,inorder,i+,end,++index);
return root;
}
};

这是一道分治的题目,用先序找到根节点,用中序找到其左右子树。

补充一个我认为比较容易理解的版本,使用python 实现:

 class Solution:
def buildTree(self, preorder: 'List[int]', inorder: 'List[int]') -> 'TreeNode':
if len(preorder)== or len(inorder)==:
return None val = preorder[]
t = TreeNode(val)
index = inorder.index(val)
t.left = self.buildTree(preorder[:index+],inorder[:index])
t.right = self.buildTree(preorder[index+:],inorder[index+:])
return t

leetcode105的更多相关文章

  1. leetcode105:Construct Binary Tree from Preorder and Inorder Traversal

    题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume t ...

  2. [Swift]LeetCode105. 从前序与中序遍历序列构造二叉树 | 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. C++版-剑指offer 面试题6:重建二叉树(Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal) 解题报告

    剑指offer 重建二叉树 提交网址: http://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6?tpId=13&tq ...

  4. 【2】【leetcode-105,106】 从前序与中序遍历序列构造二叉树,从中序与后序遍历序列构造二叉树

    105. 从前序与中序遍历序列构造二叉树 (没思路,典型记住思路好做) 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [ ...

  5. 【LeetCode105】Construct Binary Tree from Preorder and Inorder Traversal★★

    1.题目 2.思路 3.java代码 //测试 public class BuildTreeUsingInorderAndPreorder { public static void main(Stri ...

  6. LeetCode105. Construct Binary Tree from Preorder and Inorder Traversal

    题目 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = [9,3 ...

  7. leetcode105 从前序与中序遍历序列构造二叉树

    如何遍历一棵树 有两种通用的遍历树的策略: 宽度优先搜索(BFS) 我们按照高度顺序一层一层的访问整棵树,高层次的节点将会比低层次的节点先被访问到. 深度优先搜索(DFS) 在这个策略中,我们采用深度 ...

  8. Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal前序与中序构造二叉树

    根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = [9,3,15 ...

  9. leetcode105: jump-game-ii

    题目描述 给出一个非负整数数组,你最初在数组第一个元素的位置 数组中的元素代表你在这个位置可以跳跃的最大长度 你的目标是用最少的跳跃次数来到达数组的最后一个元素的位置 例如 给出数组 A =[2,3, ...

随机推荐

  1. QFileSystemModel中通过flags函数反应代码的层级思考

    Qt的Model/View设计中,有一些隐藏的代码,它们大多放在私有类里,对于类的作用非常关键,体现着Qt的整体设计思想.然而,由于它们比较隐蔽,学习起来比较繁琐,受到人们的忽视.然而,体现设计思想, ...

  2. 关于C++使用将整形转换为字符串进行格式化的问题

    最近使用CTime类获取时间,因为在时间的格式上要求做到统一,所以会对时间信息进行格式化 目的:将时间信息生成为年4位,月2位,日2位,时2位,分2位,秒2位 也就是:2017-02-18 10:42 ...

  3. input做一个开关按钮

    .mui-switch { width: 52px; height: 31px; position: relative; border: 1px solid #dfdfdf; background-c ...

  4. Spring中的@Bean注解、@Configuration注解、@Value

    1.首先是注册bean类:@Component.@Responsitory.@Controller.@Service.Configuration这些注解是把要实例化的对象装化成一个bean,放到Ioc ...

  5. c#抓屏功能在DPI缩放后,截到的图片不完整的问题

    /// <summary> /// 获取屏幕快照 /// </summary> /// <returns></returns> public stati ...

  6. MP和OMP算法

    转载:有点无耻哈,全部复制别人的.写的不错 作者:scucj 文章链接:MP算法和OMP算法及其思想 主要介绍MP(Matching Pursuits)算法和OMP(Orthogonal Matchi ...

  7. centos 7 下 nginx 1.10.3 编译安装的方法

    安装所需环境 Nginx 是 C语言 开发,建议在 Linux 上运行,当然,也可以安装 Windows 版本,本篇则使用 CentOS 7 作为安装环境. 一. gcc 安装安装 nginx 需要先 ...

  8. Centos7安装mysql5.6.29shell脚本

    创建脚本mysql.sh,直接运行sh mysql.sh #!/bin/bash if [ -d /software ] ;then cd /software else mkdir /software ...

  9. Java方法的静态绑定与动态绑定讲解(向上转型的运行机制详解)

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6554103.html 一:绑定 把一个方法与其所在的类/对象 关联起来叫做方法的绑定.绑定分为静态绑定(前期绑 ...

  10. MySQL查询不使用索引汇总 + 如何优化sql语句

    不使用索引原文 : http://itlab.idcquan.com/linux/MYSQL/918330.html MySQL查询不使用索引汇总 众所周知,增加索引是提高查询速度的有效途径,但是很多 ...