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 duplicates do not exist in the tree.

可与Construct Binary Tree from Inorder and Postorder Traversal对照来看。

前序遍历Preorder的第一个节点为根,由于没有重复值,可使用根将中序Inorder划分为左右子树。

递归下去即可。

/**
* Definition for binary tree
* 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) {
return Helper(preorder, , preorder.size()-, inorder, , inorder.size()-);
} TreeNode* Helper(vector<int> &preorder, int begin1, int end1, vector<int> &inorder, int begin2, int end2)
{
if(begin1 > end1)
return NULL;
else if(begin1 == end1)
return new TreeNode(preorder[begin1]); TreeNode* root = new TreeNode(preorder[begin1]);
int i = begin2;
for(; i <= end2; i ++)
{
if(inorder[i] == preorder[begin1])
break;
}
//inorder[i] is the root
int leftlen = i-begin2; //preorder[begin1] is root
//inorder[begin2+leftlen] is root
root->left = Helper(preorder, begin1+, begin1+leftlen, inorder, begin2, begin2+leftlen-);
root->right = Helper(preorder, begin1+leftlen+, end1, inorder, begin2+leftlen+, end2);
return root;
}
};

【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章

  1. 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  2. 【一天一道LeetCode】#105. Construct Binary Tree from Preorder and Inorder Traversal

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

  3. 【leetocde】 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. 【原创】leetCodeOj ---Construct Binary Tree from Preorder and Inorder Traversal 解题报告

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

  5. LeetCode OJ 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 ...

  6. 【LeetCode】889. Construct Binary Tree from Preorder and Postorder Traversal 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

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

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

  9. (二叉树 递归) 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 ...

随机推荐

  1. python - 在Windows系统中安装Pygame及导入Eclipse

    环境:python3.6(只有一个版本)+ windows10(64 bit)  + Eclipse+pydev python3.6安装完成后,会自带 easy_install 和 pip3,在Win ...

  2. 复制到剪切板js代码(转)

    <script type="text/javascript" language="javascript"> //复制到剪切板js代码 functio ...

  3. 开发Google Material Design风格的WPF程序

    今天在网上看到了一个Material Design风格的WPF皮肤,看上去还是挺不错的 这个项目是开源的,感兴趣的朋友可以下载试下: https://github.com/ButchersBoy/Ma ...

  4. S3C2440上LCD驱动(FrameBuffer)实例开发讲解

    一.开发环境 主  机:VMWare--Fedora 9 开发板:Mini2440--64MB Nand, Kernel:2.6.30.4 编译器:arm-linux-gcc-4.3.2 二.背景知识 ...

  5. Replace Pioneer 试用推广

    Replace Pioneer: http://www.mind-pioneer.com 目前合法长期使用Replace Pioneer的唯一方法(除了购买之外): Replace Pioneer过期 ...

  6. android开发:退出程序(对话框、两次返回键退出)

    private void exitDialog() { AlertDialog.Builder aa=new AlertDialog.Builder(this); aa.setTitle(" ...

  7. acd The Game about KILL(和约瑟夫归则一样,归律)

    Problem Description Teacher HU and his 40 students were trapped by the brigands. To show their power ...

  8. [Gradle] Gradle 构建 android 应用常见问题解决指南

    转载地址:http://www.cnblogs.com/youxilua/p/3348162.html 1: 使用最新的gradle android插件 以前我们写的时候会这么写 dependenci ...

  9. event & EventHandler

    [event & EventHandler] 在老C#中EventHandler指的是一个需要定义一个delegate,这个delegate是回调的规范.例如: public delegate ...

  10. JAVA , TOMCAT , AXIS2 环境变量配置

    想要成功配置Java的环境变量,那肯定就要安装JDK,才能开始配置的. 安装JDK 向导进行相关参数设置.如图: 正在安装程序的相关功能,如图: 选择安装的路径,可以自定义,也可以默认路径.如图: 成 ...