解题

前序遍历和中序遍历树构造二叉树

根据前序遍历和中序遍历树构造二叉树.

样例

给出中序遍历:[1,2,3]和前序遍历:[2,1,3]. 返回如下的树:

  2
/ \
1 3
注意

你可以假设树中不存在相同数值的节点

解题

上一题很类似的。

前序遍历:根左右

中序遍历:左根右

/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/**
*@param preorder : A list of integers that preorder traversal of a tree
*@param inorder : A list of integers that inorder traversal of a tree
*@return : Root of a tree
*/
public TreeNode buildTree(int[] preorder, int[] inorder) {
// write your code here
if(preorder.length != inorder.length)
return null;
return buildTree(preorder,0,preorder.length - 1,inorder,0,inorder.length - 1);
}
public int findPosition(int[] inorder,int istart,int iend,int r){
for(int i=istart;i<=iend;i++)
if(inorder[i] == r)
return i;
return -1;
}
public TreeNode buildTree(int[] preorder,int pstart,int pend,int[] inorder,int istart,int iend){
if(pstart > pend){
return null;
}
int r = preorder[pstart];
int position = findPosition(inorder,istart,iend,r);
TreeNode root = new TreeNode(r);
//左子树
// 前序遍历的左子树 起始位置:pstart + 1 结束位置:pstart + (position - 1 -istart + 1)
// 中序遍历的左子树 起始位置:istart 结束位置 position -1
root.left = buildTree(preorder,pstart + 1,pstart + (position - 1 -istart + 1),inorder,istart,position-1);
// 右子树
// 前序遍历的右子树 起始位置 pstart + (position - 1 -istart + 1) + 1 结束位置:pend
// 中序遍历的右子树 起始位置:position + 1 结束位置:iend
root.right = buildTree(preorder,pstart + (position - 1 -istart + 1) + 1,pend,inorder,position + 1,iend);
return root;
} }

lintcode :前序遍历和中序遍历树构造二叉树的更多相关文章

  1. [leetcode/lintcode 题解] 前序遍历和中序遍历树构造二叉树

    [题目描述] 根据前序遍历和中序遍历树构造二叉树. 在线评测地址: https://www.jiuzhang.com/solution/construct-binary-tree-from-preor ...

  2. LintCode-73.前序遍历和中序遍历树构造二叉树

    前序遍历和中序遍历树构造二叉树 根据前序遍历和中序遍历树构造二叉树. 注意事项 你可以假设树中不存在相同数值的节点 样例 给出中序遍历:[1,2,3]和前序遍历:[2,1,3]. 返回如下的树:    ...

  3. POJ 2255 Tree Recovery(根据前序遍历和中序遍历,输出后序遍历)

    题意:给出一颗二叉树的前序遍历和中序遍历的序列,让你输出后序遍历的序列. 思路:见代码,采用递归. #include <iostream> #include <stdio.h> ...

  4. L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...

  5. 【C++】根据二叉树的前序遍历和中序遍历重建二叉树并输出后续遍历

    /* 现在有一个问题,已知二叉树的前序遍历和中序遍历: PreOrder:GDAFEMHZ InOrder:ADEFGHMZ 我们如何还原这颗二叉树,并求出他的后序遍历 我们基于一个事实:中序遍历一定 ...

  6. java编写二叉树以及前序遍历、中序遍历和后序遍历 .

    /** * 实现二叉树的创建.前序遍历.中序遍历和后序遍历 **/ package DataStructure; /** * Copyright 2014 by Ruiqin Sun * All ri ...

  7. python数据结构之树和二叉树(先序遍历、中序遍历和后序遍历)

    python数据结构之树和二叉树(先序遍历.中序遍历和后序遍历) 树 树是\(n\)(\(n\ge 0\))个结点的有限集.在任意一棵非空树中,有且只有一个根结点. 二叉树是有限个元素的集合,该集合或 ...

  8. HDU1710---树(知前序遍历与中序遍历 求后序遍历)

    知前序遍历与中序遍历 求后序遍历 #include<iostream> #include<cstring> #include<queue> #include< ...

  9. 剑指offer面试题:输入某二叉树的前序遍历和中序遍历,输出后序遍历

    二叉树的先序,中序,后序如何遍历,不在此多说了.直接看题目描述吧(题目摘自九度oj剑指offer面试题6): 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结 ...

随机推荐

  1. android-support-xxxx.jar NoClassDefFoundError

    当你的项目出现以下红色提示的时候,要小心了,因为很可能因为这个错误而导致解释不通的异常出现. Found 2 versions of android-support-v4.jar in the dep ...

  2. shell编程基础练习

    shell作为一个人机交互的接口,在Linux中发挥着很大的作用,而shell编程则是减轻系统工程师工作的利器,这里总结一下shell编程的主要内容(趁着程序运行的空档). 本文的基本结构是shell ...

  3. php中url传递中文字符,特殊危险字符的解决方法

    php中的urldecode,base64_encode函数然后再结合自己写的替换函数来进行安全传递url中文字符,特殊危险字符. 需要在url中传递中文字符或是其它的html等特殊字符,似乎总会有各 ...

  4. JS判断鼠标从什么方向进入一个容器

    偶然将想到的一个如何判断鼠标从哪个方向进入一个容器的问题.首先想到的是给容器的四个边添加几个块,然后看鼠标进入的时候哪个块先监听到鼠标事件.不过这样麻烦太多了.google了一下找到了一个不错的解决方 ...

  5. Windows 下 Composer 与 Laravel 4 的安装

    1.安装Composer Composer是PHP的依附经管对象之一,官方网站 http://getcomposer.org/ .它支撑多种安装体式格式,对于在win下做开辟的草来说,最便捷的体式格式 ...

  6. 基于jQuery的判断iPad、iPhone、Android是横屏还是竖屏

    function orient() {if (window.orientation == 90 || window.orientation == -90) {//ipad.iphone竖屏:Andri ...

  7. 通过js获取DropDownList的选中项

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...

  8. 11G RAC 简单命令

    1.查看集群状态: [root@rac1 ~]# su - grid [grid@rac1 ~]$ crsctl check clusterCRS-4537: Cluster Ready Servic ...

  9. [Java][RCP] 引入第三方jar包时出错: XXXcannot be found XXX

    为什么会这样? 下面的博客有介绍,不在累赘 http://dengmin.iteye.com/blog/260585 这些博客貌似忘掉了一点,或者是我本地的Eclipse新建的项目Version不够高 ...

  10. iOS下日期的处理(世界标准时转本地时间)

    NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates         NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是 ...