lintcode: 中序遍历和后序遍历树构造二叉树
题目
中序遍历和后序遍历树构造二叉树
根据中序遍历和后序遍历树构造二叉树
给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2]
返回如下的树:
2
/ \
1 3
你可以假设树中不存在相同数值的节点
解题
1.后序遍历最后一个结点就是根节点,根据这个根结点把中序遍历划分开来,同时也把后续遍历划分开来
2.递归就好了
程序感觉很简单不知道怎么写的,程序来源于九章
/**
* 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 inorder : A list of integers that inorder traversal of a tree
*@param postorder : A list of integers that postorder traversal of a tree
*@return : Root of a tree
*/
private int findPosition(int[] arr, int start, int end, int key) {
int i;
for (i = start; i <= end; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
} private TreeNode myBuildTree(int[] inorder, int instart, int inend,
int[] postorder, int poststart, int postend) {
if (instart > inend) {
return null;
} TreeNode root = new TreeNode(postorder[postend]);
int position = findPosition(inorder, instart, inend, postorder[postend]); root.left = myBuildTree(inorder, instart, position - 1,
postorder, poststart, poststart + position - instart - 1);
root.right = myBuildTree(inorder, position + 1, inend,
postorder, poststart + position - instart, postend - 1);
return root;
} public TreeNode buildTree(int[] inorder, int[] postorder) {
if (inorder.length != postorder.length) {
return null;
}
return myBuildTree(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1);
}
}
Java Code
自己又实现了一遍,并加了注释
/**
* 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 inorder : A list of integers that inorder traversal of a tree
*@param postorder : A list of integers that postorder traversal of a tree
*@return : Root of a tree
*/
public TreeNode buildTree(int[] inorder, int[] postorder) {
// write your code here
if(inorder.length != postorder.length)
return null;
return buildTree(inorder,0,inorder.length -1 ,postorder,0,postorder.length -1 );
}
public int findroot(int[] inorder,int r){
for(int i=0;i<inorder.length;i++)
if(inorder[i] == r)
return i;
return -1;
}
public TreeNode buildTree(int[] inorder ,int istart,int iend,int[] postorder,int pstart,int pend){
if(istart > iend)
return null;
int r = postorder[pend];
// 跟结点
TreeNode root = new TreeNode(r);
// 找到根节点
int l = findroot(inorder,r);
// 左子树 中序遍历 起始结束位置以此是:istart l-1
//后序遍历 起始位置是:pstart 结束位置:pstart(已经占据了一个位置所以要-1) + (左子树的长度) - 1
root.left = buildTree(inorder,istart,l-1,postorder,pstart,pstart+(l-1 - istart + 1) -1);
// 右子树 中序遍历 起始结束位置:l+1 iend
// 后序遍历 起始位置:pstart + (左子树的长度) ,结束位置 pend -1
root.right = buildTree(inorder,l+1,iend,postorder,pstart + (l-1-istart+1),pend -1);
return root;
}
}
lintcode: 中序遍历和后序遍历树构造二叉树的更多相关文章
- java编写二叉树以及前序遍历、中序遍历和后序遍历 .
/** * 实现二叉树的创建.前序遍历.中序遍历和后序遍历 **/ package DataStructure; /** * Copyright 2014 by Ruiqin Sun * All ri ...
- python数据结构之树和二叉树(先序遍历、中序遍历和后序遍历)
python数据结构之树和二叉树(先序遍历.中序遍历和后序遍历) 树 树是\(n\)(\(n\ge 0\))个结点的有限集.在任意一棵非空树中,有且只有一个根结点. 二叉树是有限个元素的集合,该集合或 ...
- 根据 中序遍历 和 后序遍历构造树(Presentation)(C++)
好不容易又到周五了,周末终于可以休息休息了.写这一篇随笔只是心血来潮,下午问了一位朋友PAT考的如何,顺便看一下他考的试题,里面有最后一道题,是关于给出中序遍历和后序遍历然后求一个层次遍历.等等,我找 ...
- TZOJ 3209 后序遍历(已知中序前序求后序)
描述 在数据结构中,遍历是二叉树最重要的操作之一.所谓遍历(Traversal)是指沿着某条搜索路线,依次对树中每个结点均做一次且仅做一次访问. 这里给出三种遍历算法. 1.中序遍历的递归算法定义: ...
- LintCode2016年8月8日算法比赛----中序遍历和后序遍历构造二叉树
中序遍历和后序遍历构造二叉树 题目描述 根据中序遍历和后序遍历构造二叉树 注意事项 你可以假设树中不存在相同数值的节点 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下 ...
- 数据结构实习 - problem K 用前序中序建立二叉树并以层序遍历和后序遍历输出
用前序中序建立二叉树并以层序遍历和后序遍历输出 writer:pprp 实现过程主要是通过递归,进行分解得到结果 代码如下: #include <iostream> #include &l ...
- LintCode-72.中序遍历和后序遍历树构造二叉树
中序遍历和后序遍历树构造二叉树 根据中序遍历和后序遍历树构造二叉树 注意事项 你可以假设树中不存在相同数值的节点 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下的树: ...
- 小小c#算法题 - 11 - 二叉树的构造及先序遍历、中序遍历、后序遍历
在上一篇文章 小小c#算法题 - 10 - 求树的深度中,用到了树的数据结构,树型结构是一类重要的非线性数据结构,树是以分支关系定义的层次结构,是n(n>=0)个结点的有限集.但在那篇文章中,只 ...
- 数据结构学习-BST二叉查找树 : 插入、删除、中序遍历、前序遍历、后序遍历、广度遍历、绘图
二叉查找树(Binary Search Tree) 是一种树形的存储数据的结构 如图所示,它具有的特点是: 1.具有一个根节点 2.每个节点可能有0.1.2个分支 3.对于某个节点,他的左分支小于自身 ...
随机推荐
- Winform webBrowser 不跳转网页
private void webBrowser1_NewWindow(object sender, CancelEventArgs e) { string url = ((WebBrowser)sen ...
- mongodb 入门笔记
选择Mongo的关键是:这是一个 JSON 文档数据库. 1. Mongo 的术语 文档:一条完整的数据就是一个文档(对应于 MySQL 的一行). 集合:一组文档构成一个集合.类似 MySQL 中表 ...
- 利用ajax在javascript中获取后台的值
<script type="text/javascript"> function login() { var sa = WebForm1.Hello().value; ...
- SQL Server 数据库身份认证以及包含数据库
首先分为SQL Server 认证与Windows 身份认证. SQL Server 认证可以运行以下语句来查询 select * from sys.sql_logins 管理员可以直接修改密码,但无 ...
- Android Studio快速开发之道(各种语法糖)
现如今开发越来越追求效率和节奏,节省出时间做更多的事情,除了开发技术上的封装等,开发工具的使用技巧也是很重要的,今天就根据自己的经验来给大家介绍一下Android Studio快速开发之道. Post ...
- APACHE 403 FORBIDDEN错误的解决办法之一
打开 apache的配置文件httpd.conf,找到这段代码: Options FollowSymLinksAllowOverride NoneOrder deny,allowDeny from a ...
- [转]unable to resolve superclass of 的奇怪问题和一种解决方法!
[转]unable to resolve superclass of 的奇怪问题和一种解决方法! http://blog.csdn.net/jackymvc/article/details/90015 ...
- Python实战(1)
此次实战完全按照Python教程 - 廖雪峰的官方网站进行 首先下载windows版本的Python2.7,附上下载链接http://www.python.org/ftp/python/2.7.6/p ...
- String对象中常用的方法
String对象中常用的方法 1.charCodeAt方法返回一个整数,代表指定位置字符的Unicode编码.strObj.charCodeAt(index)说明:index将被处理字符的从零开始 ...
- JavaScript判断闰年
<html><head> <meta http-equiv="content-type" content="text/html;char ...