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

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

利用前序和中序遍历构造二叉树的思想与利用中序和后续遍历的思想一样,不同的是,全树的根节点为preorder数组的第一个元素,具体分析过程参照利用中序和后续构造二叉树。这两道题是从二叉树的前序遍历、中序遍历、后续遍历这三种遍历中选取两种方式构造二叉树,总的思路都是先确定全树根节点,然后在中序遍历中找到根节点,从而确定全树的左、右子树(题目要求没有重复元素,所以可以确定),最后以两子树为新的二叉树递归调用原函数。值得注意的是:利用三种遍历构造二叉树,要具备两条件,一、知道根节点、二,能区分左右子树,所以不能用前序遍历和后续遍历构造二叉树(不知道是否有大神可以)。代码如下

 /**
* 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)
{
int len=preorder.size();
return build(preorder,,len-,inorder,,len-);
} TreeNode *build(vector<int> &preorder,int preBeg,int preEnd,vector<int> &inorder,int inBeg,int inEnd)
{
if(preBeg>preEnd||inBeg>inEnd) return NULL;
TreeNode *root=new TreeNode(preorder[preBeg]); for(int i=;i<inorder.size();++i)
{
if(inorder[i]==preorder[preBeg])
{
root->left=build(preorder,preBeg+,preBeg+i-inBeg,inorder,inBeg,i-);
root->right=build(preorder,preBeg+i+-inBeg,preEnd,inorder,i+,inEnd);
}
}
return root;
}
};

[Leetcode] Construct binary tree from preorder and inorder travesal 利用前序和中续遍历构造二叉树的更多相关文章

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

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

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

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

  4. LeetCode:105_Construct Binary Tree from Preorder and Inorder Traversal | 根据前序和中序遍历构建二叉树 | Medium

    要求:通过二叉树的前序和中序遍历序列构建一颗二叉树 代码如下: struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode ...

  5. 105 Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树

    给定一棵树的前序遍历与中序遍历,依据此构造二叉树.注意:你可以假设树中没有重复的元素.例如,给出前序遍历 = [3,9,20,15,7]中序遍历 = [9,3,15,20,7]返回如下的二叉树:    ...

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

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

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

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

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

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

随机推荐

  1. 关于Python的多重排序

    Python预置的list.sort().sorted()方法可实现各种数组的排序,但支持的只限于一个key,如果要多重排序,目前所知的方法只有自定义了. Help on built-in funct ...

  2. MySQL☞abs函数

    abs( )函数:求出绝对值 格式: select  abs(数值)  from 表名 如下图:

  3. 第八模块:算法&设计模式、企业应用 第1章 常用算法&设计模式学习

    第八模块:算法&设计模式.企业应用 第1章 常用算法&设计模式学习

  4. HTMLTestRunner带饼图

    # -*- coding: utf-8 -*- """ A TestRunner for use with the Python unit testing framewo ...

  5. Java中二进制数与整型之间的转换

    import java.io.*; public class Test{ /** * 二进制与整型之间的转换 * @param args * @throws IOException */ public ...

  6. 系统滴答定时器(SysTick)中断配置

    系统滴答定时器(SysTick)中断配置 在STM32标准库中是通过SysTick_Config()函数配置时钟中断的,然后SysTick_Handler()函数自动定时触发其中的函数. if(Sys ...

  7. Codeforces 96D Volleyball(最短路径)

    Petya loves volleyball very much. One day he was running late for a volleyball match. Petya hasn't b ...

  8. str和repr

    在Python2.6和Python3.0以及更早的版本中,在交互式模式下的输出本质上是使用repr,因此对于一些浮点数运算,会显示很多位: 4 / 5.0 #0.8000000000000004 但是 ...

  9. java多线程三之线程协作与通信实例

    多线程的难点主要就是多线程通信协作这一块了,前面笔记二中提到了常见的同步方法,这里主要是进行实例学习了,今天总结了一下3个实例: 1.银行存款与提款多线程实现,使用Lock锁和条件Condition. ...

  10. Java中I/O流之处理流类型

    节点流:一个管道直接连接到数据源上面: 处理流:套在别的管道上面的管道: 处理流类型: [注]:在字符流中的OuPutStreamReader写错了,应该是:OutputStreamWriter