根据一棵树的前序遍历与中序遍历构造二叉树。

注意:

你可以假设树中没有重复的元素。

例如,给出

前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = [9,3,15,20,7]

返回如下的二叉树:

3 / \ 9 20 / \ 15 7

class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder)
{
if(preorder.size() == 0)
return NULL;
if(preorder.size() == 1)
return new TreeNode(preorder[0]);
int iroot = preorder[0];
int ipos = 0;
for(int i = 0; i < inorder.size(); i++)
{
if(inorder[i] == iroot)
{
ipos = i;
break;
}
}
TreeNode *root = new TreeNode(iroot);
vector<int> v1(preorder.begin() + 1, preorder.begin() + ipos + 1);
vector<int> v2(inorder.begin(), inorder.begin() + ipos);
vector<int> v3(preorder.begin() + ipos + 1, preorder.end());
vector<int> v4(inorder.begin() + ipos + 1, inorder.end());
root ->left = buildTree(v1, v2);
root ->right = buildTree(v3, v4);
return root;
}
};

Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal前序与中序构造二叉树的更多相关文章

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

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

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

  4. C++版-剑指offer 面试题6:重建二叉树(Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal) 解题报告

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

  5. Construct Binary Tree from Preorder and Inorder Traversal

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

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

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

  8. 【题解二连发】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 ...

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

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

随机推荐

  1. html自定义分页

    public class MyPager { /// <summary> /// 每一页数据的条数 /// </summary> public int PageSize { g ...

  2. vue 返回上一页

    参考:https://www.cnblogs.com/chenguiya/p/9118265.html 注:需为history模式 方法一: @click="back" back( ...

  3. JS控制语句 编程练习 学生数据,分别是姓名、性别、年龄和年级,接下来呢,我们要利用JavaScript的知识挑出其中所有是大一的女生的的名字哦。

    编程练习 在一个大学的编程选修课班里,我们得到了一组参加该班级的学生数据,分别是姓名.性别.年龄和年级,接下来呢,我们要利用JavaScript的知识挑出其中所有是大一的女生的的名字哦. 学生信息如下 ...

  4. Atcoder arc096

    C:Half and Half 几个if语句贪心算一算就好了 #include<cstdio> #include<algorithm> using namespace std; ...

  5. 查看java资源的占用

    1,使用命令top -p <pid> ,显示你的java进程的内存情况,pid是你的java进程号,比如1232,按H,获取每个线程的内存情况3,找到内存和cpu占用最高的线程pid,比如 ...

  6. pg总览

    一.编译安装初始化等 ./configure --prefix=/release --with-openssl --without-ldap --with-libxml - -enable-threa ...

  7. 廖雪峰Java12maven基础-1maven入门-2依赖管理

    maven 如果我们的项目依赖第三方的jar包: Commons Logging发布的jar包在那里下载? 使用Log4j需要哪些jar包 其他依赖:junit,Javamail,MySQL驱动... ...

  8. Linux 启动出现 busybox vx.x.xx built-in shell 的问题

    可能是磁盘检测错误,尤其出现在未安全关机或者磁盘损坏之后. 解决办法: 1.在选择启动项目时,选中第一项,如: ubuntu 8.04kernl.2.6.22-16-generic 2.按E 进入编辑 ...

  9. 用JS写的一个简单的时钟

    没什么技术含量,单纯的想传上去.手痒了 <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...

  10. request与session的区别

    request对象和session对象的最大区别是生命周期与范围. request request范围较小一些,只是一个请求. request对象的生命周期是针对一个客户端(说确切点就是一个浏览器应用 ...