原题

题意:

根据先序和中序得到二叉树(假设无重复数字)

思路:

先手写一次转换过程,得到思路。

即从先序中遍历每个元素,(创建一个全局索引,指向当前遍历到的元素)在中序中找到该元素作为当前的root,以该节点左边所有元素作为当前root的左支,右同理。

重复分别对左右边所有元素做相同处理。

class Solution
{
public:
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder)
{
int pos = 0;
return buildBinary(preorder, inorder, 0, preorder.size(), pos);
} private:
TreeNode *buildBinary(vector<int> &preorder, vector<int> &inorder, int beg,
int end, int &pos)
{
TreeNode *node = NULL;
if (beg < end)
{
int i = 0;
for (i = beg; i < end; i++)
{
if (preorder[pos] == inorder[i])
break;
} ++pos;
node = new TreeNode(inorder[i]);
node->left = buildBinary(preorder, inorder, beg, i, pos);
node->right = buildBinary(preorder, inorder, i + 1, end, pos);
}
return node;
}
};

[leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal (Medium)的更多相关文章

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

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

    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 由前序和中序遍历建立二叉树 C++

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

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

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

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

  8. leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal,剑指offer 6 重建二叉树

    不用迭代器的代码 class Solution { public: TreeNode* reConstructBinaryTree(vector<int> pre,vector<in ...

  9. 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

随机推荐

  1. Qt:解析命令行(使用QCommandLineOption和QCommandLineParser)

    Qt从5.2版开始提供了两个类QCommandLineOption和QCommandLineParser来解析应用的命令行参数. 一.命令行写法命令行:"-abc" 在QComma ...

  2. c# 将字符串转换为指定类型的值

    private object GetValueByProperty(string key, string value, ref Type typeValue) { Type t = typeof(T) ...

  3. c# html网页源代码浏览器显示

    //环境VS2008,WIN7SP1 //背景:人人网自动登陆,需要把读取到的html源代码显示出来, //test.txt 为html源代码 private void Form1_Load(obje ...

  4. delphi7 xml通用解析转换为stringgrid

    对于有n多记录的xml,可以填充到stringgrid中 其中 vkeynode 为 xml中 重复节点 function CommonAnalyzeXml(vxml,vkeynode: string ...

  5. Oracle11超详细安装教程和配置

    这篇博客主要是介绍一下Oracle数据的安装过程和简单的配置,帮助大家可以简单的让Oracle运行起来,只是一个基础的教程. 准备工作: 如果你以前装过Oracle数据库,而且安装目录要改变请先打开注 ...

  6. 原生Js封装的产品图片360度展示

    挺简单的一段程序,但是效果不错: 1.把需要展示的36张图片先预加载到浏览器缓存里 2.给展示图片的div添加方法 3.通过鼠标左右移动的像素转换图片 在线效果预览:http://jsfiddle.n ...

  7. Silverlight ItemsControl详细解析+解惑

    Silverlight最强大的地方就在于定义控件了,Silverlight提供了非常灵活和高效的控件定义方式,几乎可以实现任何复杂的控件实现,对于快速开发应用程序有着重要的意义.在Silverligh ...

  8. 09 audio和vedio标签

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...

  9. vue项目接入api接口

    我们在做项目时,一切基础在于数据上面,所以接入api接口是关键. 访问接口是我们会遇到跨域,而,vue-cli给我们提供了反向代理,所以我们只需要配置一下就可以了. 在config文件中找到index ...

  10. 对比Hashtable,HashMap,TreeMap,谈谈对HashMap的理解

    都实现了Map接口,存储的内容是基于key-value的键值对映射,一个映射不能有重复的键,一个键最多只能映射一个值. 1.初始化的时候:HashTable在不指定容量的情况下的默认容量是11,且不要 ...