剑指Offer-【面试题06:重建二叉树】
package com.cxz.question6;
/*
* 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
* 例如:前序遍历序列{ 1, 2, 4, 7, 3, 5, 6, 8}和中序遍历序列{4, 7, 2, 1, 5, 3, 8,6},
* 重建出下图所示的二叉树并输出它的头结点。
* */
public class Demo6 {
/**
* 二叉树的节点类
* @author CXZ
*
*/
public static class BinaryTreeNode {
int value;
BinaryTreeNode left;
BinaryTreeNode right;
}
public static BinaryTreeNode construct(int[] preorder, int[] inorder) {
) {
return null;
}
, preorder.length - , inorder, , inorder.length - );
}
/**
*
* @param preorder 前序遍历
* @param ps 前序遍历的开始位置
* @param pe 前序遍历的结束位置
* @param inorder 中序遍历
* @param is 中序遍历的开始位置
* @param ie 中序遍历的结束位置
* @return
*/
public static BinaryTreeNode construct(int[] preorder, int ps, int pe, int[] inorder, int is, int ie) {
if (ps > pe) {
return null;
}
// 取前序遍历的第一个数字,就是当前的根结点
int value = preorder[ps];
int index = is;
// 在中序遍历的数组中找根结点的位置
while (index <= ie && inorder[index] != value) {
index++;
}
// 如果在整个中序遍历的数组中没有找到,说明输入的参数是不合法的,抛出异常
if (index > ie) {
throw new RuntimeException("Invalid Input");
}
// 创建当前的根结点,并且为结点赋值
BinaryTreeNode node = new BinaryTreeNode();
node.value = value;
// 递归构建当前根结点的左子树,左子树的元素个数:index-is+1个
// 左子树对应的前序遍历的位置在[ps+1, ps+index-is]
// 左子树对应的中序遍历的位置在[is, index-1]
node.left = construct(preorder, ps + , ps + index - );
// 递归构建当前根结点的右子树,右子树的元素个数:ie-index个
// 右子树对应的前序遍历的位置在[ps+index-is+1, pe]
// 右子树对应的中序遍历的位置在[index+1, ie]
node.right = construct(preorder, ps + index - , pe, inorder, index + , ie);
//返回创建的根节点
return node;
}
//中序遍历二叉树
public static void printTree(BinaryTreeNode root) {
if (root != null) {
printTree(root.left);
System.out.print(root.value + " ");
printTree(root.right);
}
}
// 普通二叉树
// 1
// / \
// 2 3
// / / \
// 4 5 6
// \ /
// 7 8
private static void test1() {
, , , , , , , };
, , , , , , , };
BinaryTreeNode root = construct(preorder, inorder);
printTree(root);
}
// 所有结点都没有右子结点
// 1
// /
// 2
// /
// 3
// /
// 4
// /
// 5
private static void test2() {
, , , , };
, , , , };
BinaryTreeNode root = construct(preorder, inorder);
printTree(root);
}
// 所有结点都没有左子结点
// 1
// \
// 2
// \
// 3
// \
// 4
// \
// 5
private static void test3() {
, , , , };
, , , , };
BinaryTreeNode root = construct(preorder, inorder);
printTree(root);
}
// 树中只有一个结点
private static void test4() {
};
};
BinaryTreeNode root = construct(preorder, inorder);
printTree(root);
}
// 完全二叉树
// 1
// / \
// 2 3
// / \ / \
// 4 5 6 7
private static void test5() {
, , , , , , };
, , , , , , };
BinaryTreeNode root = construct(preorder, inorder);
printTree(root);
}
// 输入空指针
private static void test6() {
construct(null, null);
}
// 输入的两个序列不匹配
private static void test7() {
, , , , , , };
, , , , , , };
BinaryTreeNode root = construct(preorder, inorder);
printTree(root);
}
public static void main(String[] args) {
test1();
System.out.println();
test2();
System.out.println();
test3();
System.out.println();
test4();
System.out.println();
test5();
System.out.println();
test6();
System.out.println();
test7();
}
}
剑指Offer-【面试题06:重建二叉树】的更多相关文章
- 剑指offer面试题6 重建二叉树(c)
- 剑指offer面试题6 重建二叉树(java)
注:(1)java中树的构建 (2)构建子树时可以直接利用Arrays.copyOfRange(preorder, from, to),这个方法是左开右闭的 package com.xsf.SordF ...
- 剑指Offer:面试题6——重建二叉树(java实现)
问题描述:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不包含重复的数字. 例如: 输入:前序{1,2,4,7,3,5,6,8},中序{4,7,2,1 ...
- C++版 - 剑指Offer 面试题39:二叉树的深度(高度)(二叉树深度优先遍历dfs的应用) 题解
剑指Offer 面试题39:二叉树的深度(高度) 题目:输入一棵二叉树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度.例如:输入二叉树 ...
- 剑指Offer - 九度1385 - 重建二叉树
剑指Offer - 九度1385 - 重建二叉树2013-11-23 23:53 题目描述: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的 ...
- 剑指offer_面试题6_重建二叉树(分解步骤,逐个击破)
题目:输入某二叉树的前序遍历和中序遍历的结果.请重建出该二叉树.如果输入的前序遍历和中序遍历的结果中都不含反复的数字. 比如:输入前序遍历 {1,2,4,7,3,5,6,8} 和中序遍历序列 {4,7 ...
- 剑指offer第二版-7.重建二叉树
描述:输入某二叉树的前序遍历和中序遍历结果,重建该二叉树.假设前序遍历或中序遍历的结果中无重复的数字. 思路:前序遍历的第一个元素为根节点的值,据此将中序遍历数组拆分为左子树+root+右子树,前序遍 ...
- 剑指offer【04】- 重建二叉树(java)
题目:重建二叉树 考点:树 题目描述:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6, ...
- 剑指offer(4)重建二叉树
题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...
- 剑指offer——面试题8:二叉树的下一个节点
// 面试题8:二叉树的下一个结点 // 题目:给定一棵二叉树和其中的一个结点,如何找出中序遍历顺序的下一个结点? // 树中的结点除了有两个分别指向左右子结点的指针以外,还有一个指向父结点的指针. ...
随机推荐
- 将list集合的元素按照添加顺序的倒序进行排列取出
1.方法 Collections.reverse(list); 2.代码示例 /** * 从redis中将现场状态的记录全部取出 * @param aucId * @return */ @Reques ...
- Python Day14
HTML HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这样就可以让浏 ...
- 百度地图api调用
<script type="text/javascript" src="http://api.map.baidu.com/api?key=&v=1.1&am ...
- IntelliJ IDEA 15,16 win 7 64位安装包以及注册码 百度云盘
不知道发出来,有用没有,要是官网下载不了的话,可以用我的这个哦,虽然不是最新的. ideaIU-162.1447.21 ideaIU-15.0.2 win7 64系统的安装包 链接:http://p ...
- Fedora中显示windows下的文件
目录 一些预备知识: 在中国windows的编码是本地编码 , 即GBK,GB2312,GB18030等 GBK 也就是windows-986 Windows现在只支持Unicode (UTF-16 ...
- MATLAB读取一张RGB图片转成YUV格式
1.读入照片 控制输出的标志定义 clc;close all;clear YES = 1; NO = 0; %YES表示输出该文件,请用户配置 yuv444_out_txt = 1; yuv444_o ...
- fedora配置163为yum的源
一种方法: 1.下载 http://mirrors.163.com/.help/fedora-163.repo 和 http://mirrors.163.com/.help/fedora-updat ...
- HDU4411 最小费用流
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4411 floyd处理出最短路 每个点拆为i.i+n,i到i+n连一条容量为1,费用为负无穷的边,代表这个城 ...
- .NET积累
2016-10-27 给视图中的select赋值: 控制器: public ActionResult Add() { List<SelectListItem> ClassName = ne ...
- ajax获取json数据 for select2
json数据“a.json” [ { "id": "1", "text": "张三" }, { "id&quo ...