原题

题意:

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

思路:

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

即从先序中遍历每个元素,(创建一个全局索引,指向当前遍历到的元素)在中序中找到该元素作为当前的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. D7下FastMM的使用

    原文出处:http://hi.baidu.com/showwindows/blog/item/5b7ac601c487c605728da573.html FastMM 快速MM:-),在D2006和2 ...

  2. 11g Oracle导出表 默认不导出数据为空的表解决

    11g oracle导出表时会默认不导出数据为空 1.Oracle11g默认对空表不分配segment,故使用exp导出Oracle11g数据库时,空表不会导出.  2.设置deferred_segm ...

  3. Awesome Go (http://awesome-go.com/)

    A curated list of awesome Go frameworks, libraries and software. Inspired by awesome-python. Contrib ...

  4. linux 磁盘控件找到大文件

    df -lh Used:已经使用的空间 Avail:可以使用的空间 Mounted on:挂载的目录 然后找到大文件 du是linux下用看查看磁盘的命令 下面我们先一个目录的来查看空间占用情况 du ...

  5. SYN591-B型 转速表

       SYN591-B型 转速表 光电转速表数显转速表智能转速表使用说明视频链接: http://www.syn029.com/h-pd-249-0_310_44_-1.html 请将此链接复制到浏览 ...

  6. vue.js异步上传文件前后端代码

    上传文件前端代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type&q ...

  7. 支持向量机 (二): 软间隔 svm 与 核函数

    软间隔最大化(线性不可分类svm) 上一篇求解出来的间隔被称为 "硬间隔(hard margin)",其可以将所有样本点划分正确且都在间隔边界之外,即所有样本点都满足 \(y_{i ...

  8. Python自学day-1

    一.Python介绍 1.python擅长领域:     WEB开发:Django. pyramid. Tornado. Bottle. Flask. WebPy     网络编程:Twisted(牛 ...

  9. VirtualBox基础使用

    VirtualBox基础使用 VirtualBox相对VMware来说是轻量级的虚拟软件, 最关键的是VirtualBox是开源免费的. 配置全局选项 点击管理-->全局设定, 进入设置界面. ...

  10. Smobiler控件的使用:ListView的数据绑定及实现多选

    环境 SmobilerDesigner 4.7 Visual Studio 2010以上 正文 listview绑定数据 打开Visual Studio ,新建一个SmobilerApplicatio ...