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

/**
* 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. java之Comparator与Comparable

    转自:http://blog.csdn.net/zhangerqing 当需要排序的集合或数组不是单纯的数字型时,通常可以使用Comparator或Comparable,以简单的方式实现对象排序或自定 ...

  2. python编写Mysql自动备份脚本

    1、先写一个python脚本 vim /opt/mysql_dump.py #!/usr/bin/env python# -*- coding:utf-8 -*-import osimport tim ...

  3. poj 1226

    跟3294比较类似,但是不需要输出具体的串,比较简单,只要把串反转连接上去解法就一样了. #include <iostream> #include <cstdio> #incl ...

  4. JAVA向文件中追加内容(转)

    向文件尾加入内容有多种方法,常见的方法有两种: RandomAccessFile类可以实现随机访问文件的功能,可以以读写方式打开文件夹的输出流 public void seek(long pos)可以 ...

  5. BTrace系列

    http://blog.csdn.net/mgoann/article/category/473844 http://www.cnblogs.com/serendipity/archive/2012/ ...

  6. mysql 交互式连接和非交互式连接

    交互式客户端定义为在mysql_real_connect()中使用CLIENT_INTERACTIVE选项的客户端 mysql_real_connect() 函数介绍 函数原型描述: MYSQL *m ...

  7. IHttpModule实现URL重写

    1.用自定义IHttpModule实现URL重写 一般来说,要显示一些动态数据总是采用带参数的方式,比如制作一个UserInfo.aspx的动态页面用于显示系统的UserInfo这个用户信息表的数据, ...

  8. java命名规则

    java变量命名是不能以#开头的.Java变量的命名规则如下:$ .字母.下划线开头都行,不能以数字开头 后面的可以是数字.字母.下划线, 其他的命名方式,都会报错,且不能运行. 以字母.下划线( _ ...

  9. 分享一下常用的hosts列表

    #以下的hosts能够保证基本的谷歌服务不受影响,其他网站则不一定能够顺利访问,Youtube视频无法播放,但是可以顺利上传视频 #Thanks smarthosts  ,  老夏 ,Humorce  ...

  10. [弹出消息] C#MessageBox帮助类 (转载)

    点击下载 MessageBox.rar 主要功能如下所示1.显示消息提示对话框 2.控件点击 消息确认提示框 3.显示消息提示对话框,并进行页面跳转 4.输出自定义脚本信息 /// <summa ...