[Leetcode] Construct binary tree from preorder and inorder travesal 利用前序和中续遍历构造二叉树
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 利用前序和中续遍历构造二叉树的更多相关文章
- 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 ...
- 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 ...
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- LeetCode:105_Construct Binary Tree from Preorder and Inorder Traversal | 根据前序和中序遍历构建二叉树 | Medium
要求:通过二叉树的前序和中序遍历序列构建一颗二叉树 代码如下: struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode ...
- 105 Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树
给定一棵树的前序遍历与中序遍历,依据此构造二叉树.注意:你可以假设树中没有重复的元素.例如,给出前序遍历 = [3,9,20,15,7]中序遍历 = [9,3,15,20,7]返回如下的二叉树: ...
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- [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 ...
- [leetcode]Construct Binary Tree from Preorder and Inorder Traversal @ Python
原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意:根 ...
- 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 ...
随机推荐
- apache Subversion 直接支持LDAP域群组
如果你的Subversion已经用apache的ldap支持用户认证功能,你是否常常在想,既然都用ldap支持认证,为什么不直接支持域群组, 反而在authz文件里面一个一个的手工定义,或者有人用脚本 ...
- 获取Chromium代码以及编译
获取和编译Chromium必须自备梯子,最好是购买一个稳定的V*P*N,喜欢折腾的可以使用类似shadowsock的代理(需要设置google文档). 英文版教程文档可以参考这个界面,下面详细说Win ...
- mysql新手进阶02
云想衣裳花想容,春风拂槛露华浓. 若非群玉山头见,会向瑶台月下逢. 现在有一教学管理系统,具体的关系模式如下: Student (no, name, sex, birthday, class) Tea ...
- Objective-C 封装 继承 多态
封装 #import <Foundation/Foundation.h> @interface Person : NSObject { //@public int _age; } - (v ...
- <cfenv>(fenv.h) _c++11
头文件 <cfenv>(fenv.h) c++11 浮点环境 这个头文件声明了一系列的函数和宏去访问浮点环境,以及特殊的类型. 浮点环境维护一系列的状态标志(status flags)和具 ...
- redis集群搭建(伪集群)
1.准备工作 去官网下载好你想要安装的redis版本,下载链接 2.搭建步骤 输入命令yum install gcc-c++安装好gcc环境,将下载好的redis安装包上传到 /usr/local 解 ...
- 【转】一款已上市MMO手游地图同步方案总结
转自游戏开发主席 1. 客户端地图格子的相关知识 在2.5D的MMO游戏里,角色是通过3D的方式渲染,2D的地图是通过2D的方式显示,所以在客户端一般会有三个坐标系: a) 3D坐标系:所有需要3D渲 ...
- c# 计算两个时间的时间差
//计算2个日期之间的天数差 DateTime dt1 = Convert.ToDateTime("2007-8-1"); DateTime dt2 = Convert.ToDat ...
- 再学习Webform页面生命周期
参考文章: 在vs2010,新建一个aspx页面,页面头部有一行代码: <%@ Page Language="C#" AutoEventWireup="true&q ...
- JDK源码分析 – LinkedList
LinkedList类的申明 public class LinkedList<E> extends AbstractSequentialList<E> implements L ...