Given preorder and inorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

此题目有两种解决思路:

1)递归解决(比较好想)按照手动模拟的思路即可

2)非递归解决,用stack模拟递归

class Solution {
public:
TreeNode *buildTree(vector<int>& preorder, int pre_left,int pre_right,
vector<int>& inorder, int in_left, int in_right){
if(pre_left > pre_right || in_left > in_right) return NULL;
TreeNode *root = new TreeNode(preorder[pre_left]);
int index = in_left;
for( ; index <= in_right; ++ index ) if(inorder[index] == preorder[pre_left]) break;
int left_cnt = index-in_left;
root->left = buildTree(preorder,pre_left+,pre_left+left_cnt,inorder,in_left,index-);
root->right = buildTree(preorder,pre_left+left_cnt+,pre_right, inorder, index+,in_right);
return root;
} TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
if(preorder.size() == ) return NULL;
else return buildTree(preorder,,preorder.size()-, inorder,,inorder.size()-);
}
};

递归解决

Leetcode Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章

  1. LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告

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

  2. [LeetCode] 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——Construct Binary Tree from Preorder and Inorder Traversal

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

  4. [leetcode]Construct Binary Tree from Preorder and Inorder Traversal @ Python

    原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意:根 ...

  5. Leetcode: Construct Binary Tree from Preorder and Inorder Traversal, Construct Binary Tree from Inorder and Postorder Traversal

    总结: 1. 第 36 行代码, 最好是按照 len 来遍历, 而不是下标 代码: 前序中序 #include <iostream> #include <vector> usi ...

  6. LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

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

  8. 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...

  9. 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...

随机推荐

  1. PHP面向对象——重写与重载

    重写/覆盖           override 指:子类重写了父类的同名方法 class Human{    public function say($name){           echo $ ...

  2. 昨晚把家里的ie升级到11

    其实网上有些东西是实用的,不过之前的一次锁屏唤醒机器死机我就强制关机了,昨天把大部分驱动升级.

  3. ***LINUX添加PHP环境变量:CentOS下将php和mysql命令加入到环境变量中

    CentOS系统下如何将PHP和mysql命令加入到环境变量中,在Linux CentOS系统上 安装完php和MySQL后,为了使用方便,需要将php和mysql命令加到系统命令中,如果在没有添加到 ...

  4. 学习ASP.NET缓存机制

    缓存是大型BS架构网站的性能优化通用手段,之前知道有这个概念,并且也知道很重要,但是一直没静下心来了解.这次借着学习PetShop源码的机会熟悉一下ASP.NET基本的缓存机制(生产环境中的真实缓存有 ...

  5. 使用SQL语句向已有数据表添加约束

    如果向存在数据的表里添加约束,有可能会出现数据不符合检查约束而造成添加约束失败. 如: 这是一个表,为身份证号添加检查约束. USE DEmo--指向当前操作的数据库 GO ALTER TABLE E ...

  6. wp8 入门到精通 Animation 背景加字体颜色从下向上变化颜色效果

    <phone:PhoneApplicationPage.Resources> <Style x:Key="ButtonStyle1" TargetType=&qu ...

  7. hdu 5033 单调栈 ****

    看出来是单调栈维护斜率,但是不会写,2333,原来是和询问放在一起的 #include <iostream> #include <cstdio> #include <cs ...

  8. unity3D与网页的交互---做项目的一点总结

    (来自博客园) 由于项目需要,要求用unity来展示三维场景,并在三维中能够方便的查询数据库等.一开始尝试在unity中直接连接数据库,当时连的xml,然而每次发布成网页后都会出现路径找不到等问题,所 ...

  9. 遍历进程活动链表(ActiveProcessLinks)、DKOM隐藏进程

    1.EPROCESS结构体 EPROCESS块来表示.EPROCESS块中不仅包含了进程相关了很多信息,还有很多指向其他相关结构数据结构的指针.例如每一个进程里面都至少有一个ETHREAD块表示的线程 ...

  10. 【maven】maven各种奇葩问题

    问题1:Could not calculate build plan: Plugin org.apache... 不能成功创建maven项目 解决方法1: http://repo1.maven.org ...