有二叉树的前序遍历和后序遍历,构造二叉树

/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
HashMap<Integer,Integer> map = new HashMap<Integer,Integer> ();
for(int i=0;i<inorder.length;i++){
map.put(inorder[i], i);
}

return build(map,preorder,0,preorder.length-1,inorder,0,inorder.length-1);
}
private static TreeNode build(HashMap<Integer,Integer> map, int[] preorder,int ps,int pe,int[] inorder,int is,int ie) {
if(ps>pe) return null;
TreeNode root=new TreeNode(preorder[ps]);
if(ps==pe) return root;
int i=map.get(preorder[ps]);
int leftlength=i-is;
root.left=build(map,preorder,ps+1,ps+leftlength,inorder,is,i-1);
root.right=build(map,preorder,ps+i+1-is,pe,inorder,i+1,ie);
return root;
}
}

build tree的更多相关文章

  1. Build Tree View Structure for SharePoint List Data

    博客地址 http://blog.csdn.net/foxdave 此文参考自->原文链接 版权归原作者所有,我只是进行一下大致的翻译 应坛友要求,帮助验证一下功能. SharePoint列表数 ...

  2. UVa 112 Tree Summing

    题意: 计算从根到叶节点的累加值,看看是否等于指定值.是输出yes,否则no.注意叶节点判断条件是没有左右子节点. 思路: 建树过程中计算根到叶节点的sum. 注意: cin读取失败后要调用clear ...

  3. RPMForge——Quick Start build system

    How to setup multimedia on CentOS-5 CentOS ships with basic sound support for audio content encoded ...

  4. PAT1110:Complete Binary Tree

    1110. Complete Binary Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  5. PAT1064: Compelte Binary Search Tree

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  6. 【cogs 775】山海经 ——Segment Tree

    题目链接:      TP 题解:   我数据结构真心是弱啊= =. 线段树好厉害啊,一直不会区间最大连续和,今天刚学习了一下233. 维护前缀最大和后缀最大,越界最大(?),再维护一个区间最大,瞎搞 ...

  7. cmd & tree & bash

    cmd & tree & bash bug E: Unable to locate package tree solution # 1. update $ sudo apt-get u ...

  8. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  9. 105 + 106. Construct Binary Tree from Preorder and Inorder Traversal (building trees)

    Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...

随机推荐

  1. Android iOS Dribbble风格边栏菜单实现

    随着IOS7的推出,大量移动应用也开始进行了重新设计.,开始应用大量的扁平化.可以说现在IOS和Android的风格设计方面确实是在逐渐地靠拢. ReisdeMenu 创意灵感来自于Dribbble( ...

  2. Eclipse中SVN的安装步骤(两种)和使用方法[转载]

    一.给Eclipse安装SVN,最常见的有两种方式:手动方式和使用安装向导方式.具体步骤如下: 方式一:手动安装 1.从官网下载site-1.6.9.zip文件,网址是:subclipse.tigri ...

  3. 蜗牛—JSP学习之JavaBean初识

    初识: <%@ page language="java" import="java.util.*" pageEncoding="utf-8&qu ...

  4. Pilin —— 一个基于Xmpp openfire smack的即时聊天工具

    https://github.com/whfcomm/Pilin

  5. struts2 相关知识

    struts2学习笔记 1.struts.properties struts.properties 是可以不要的!!!因为 struts.xml文件中 有 <constant> 这个节点, ...

  6. WCF - 学习总目录

    WCF - 基础 WCF - 地址 WCF - 绑定 WCF - 绑定后续之自定义绑定 WCF - 契约 WCF - 序列化 WCF - 消息 WCF - 实例与会话 WCF - REST服务

  7. 调试php的soapCient

    try { import('@.Ext.xml'); header("Content-Type:text/html; charset=utf-8"); $soap = new So ...

  8. 佛主保佑,永无bug

    /*                    _ooOoo_                   o8888888o                   88" . "88      ...

  9. mysql-distinct去重、mysql-group&nbsp;…

    一.MYSQL-distinct用法 在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记 ...

  10. Java-struts2 之中文乱码问题

    中文乱码问题,是个很麻烦的问题,有时候你发现,你表单页面的编码是UTF-8 Stutrst.xml也有这么一句话 <constant name="struts.i18n.encodin ...