/**************************************************************
* Copyright (c) 2016, 北京郵電大學
* All rights reserved.
* 版 本 號:v1.0
* 題目描述:重建二叉樹
* 輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二叉樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重複數字。
* 例如,輸入前序遍歷序列{1,2,4,7,3,5,6,8}和中序遍歷序列{4,7,2,1,5,3,8,6},則重建出圖中所示的二叉樹並輸出它的頭節點。
* 輸入描述:無
* 程序輸出: 4 7 2 1 5 3 8 6 Exception in thread "main"
* 5 4 3 2 1
* 1 2 3 4 5
* 1
* 4 2 5 1 6 3 7
* 問題分析:無
* 算法描述:無
* 完成時間:2016-10-30
***************************************************************/
package org.marsguo.offerproject06;
/*
定義二叉樹節點
*/
class TreeNode{
int val;
public TreeNode left = null;
public TreeNode right = null;
}
class SolutionMethod1{
/**
* 輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二節樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。
*
* @param preorder 前序遍歷
* @param inorder 中序遍歷
* @return 樹的根結點
*/
public TreeNode BinaryTreeFunction(int[] preorder,int[] inorder){
if(preorder == null || inorder == null ||
preorder.length != inorder.length || inorder.length < 1){
return null;
}
return ConstructCore(preorder,0,preorder.length - 1,inorder,0,inorder.length - 1);
}
/**
* 輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二節樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。
*
* @param preorder 前序遍歷
* @param ps 前序遍歷的開始位置
* @param pe 前序遍歷的結束位置
* @param inorder 中序遍歷
* @param is 中序遍歷的開始位置
* @param ie 中序遍歷的結束位置
* @return 樹的根結點
*/
public TreeNode ConstructCore(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");
}
TreeNode node = new TreeNode(); // 創建當前的根結點,並且爲結點賦值
node.val = value;
/*
遞歸構建當前根結點的左子樹,左子樹的元素個數:index-is+1個
左子樹對應的前序遍歷的位置在[ps+1, ps+index-is]
左子樹對應的中序遍歷的位置在[is, index-1]
*/
node.left = ConstructCore(preorder, ps + 1, ps + index - is, inorder, is, index - 1);
/*遞歸構建當前根結點的右子樹,右子樹的元素個數:ie-index個
右子樹對應的前序遍歷的位置在[ps+index-is+1, pe]
右子樹對應的中序遍歷的位置在[index+1, ie]
*/
node.right = ConstructCore(preorder, ps + index - is + 1, pe, inorder, index + 1, ie);
// 返回創建的根結點
return node;
}
public void printTree(TreeNode root){ // 中序遍歷二叉樹
if(root != null){
printTree(root.left);
System.out.print(root.val + " ");
printTree(root.right);
}
//System.out.println();
}
// 普通二叉樹
// 1
// / \
// 2 3
// / / \
// 4 5 6
// \ /
// 7 8
public void test1() {
int[] preorder = {1, 2, 4, 7, 3, 5, 6, 8};
int[] inorder = {4, 7, 2, 1, 5, 3, 8, 6};
TreeNode root = BinaryTreeFunction(preorder, inorder);
printTree(root);
}
// 所有結點都沒有右子結點
// 1
// /
// 2
// /
// 3
// /
// 4
// /
// 5
public void test2() {
int[] preorder = {1, 2, 3, 4, 5};
int[] inorder = {5, 4, 3, 2, 1};
TreeNode root = BinaryTreeFunction(preorder, inorder);
printTree(root);
}
// 所有結點都沒有左子結點
// 1
// \
// 2
// \
// 3
// \
// 4
// \
// 5
public void test3() {
int[] preorder = {1, 2, 3, 4, 5};
int[] inorder = {1, 2, 3, 4, 5};
TreeNode root = BinaryTreeFunction(preorder, inorder);
printTree(root);
}
// 樹中只有一個結點
public void test4() {
int[] preorder = {1};
int[] inorder = {1};
TreeNode root = BinaryTreeFunction(preorder, inorder);
printTree(root);
}
// 完全二叉樹
// 1
// / \
// 2 3
// / \ / \
// 4 5 6 7
public void test5() {
int[] preorder = {1, 2, 4, 5, 3, 6, 7};
int[] inorder = {4, 2, 5, 1, 6, 3, 7};
TreeNode root = BinaryTreeFunction(preorder, inorder);
printTree(root);
}
// 輸入空指針
public void test6() {
BinaryTreeFunction(null, null);
}
// 輸入的兩個序列不匹配
public void test7() {
int[] preorder = {1, 2, 4, 5, 3, 6, 7};
int[] inorder = {4, 2, 8, 1, 6, 3, 7};
TreeNode root = BinaryTreeFunction(preorder, inorder);
printTree(root);
}
}
public class RebuildBinaryTree {
public static void main(String[] args){
SolutionMethod1 solution1 = new SolutionMethod1();
solution1.test1();
System.out.println();
solution1.test2();
System.out.println();
solution1.test3();
System.out.println();
solution1.test4();
System.out.println();
solution1.test5();
System.out.println();
solution1.test6();
System.out.println();
solution1.test7();
System.out.println();
}
}
劍指Offer面試題6:重建二叉樹 Java實現
題目:重建二叉樹
輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二叉樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重複數字。例如,輸入前序遍歷序列{1,2,4,7,3,5,6,8}和中序遍歷序列{4,7,2,1,5,3,8,6},則重建出圖中所示的二叉樹並輸出它的頭節點。
算法分析:
在二叉樹的前序遍歷序列中,第一個數字總是樹的根節點的值。但在中序遍歷中,根節點的值在序列的中間,左子樹的結點的值位於根節點的值的左邊,而右子樹的結點的值位於根節點的右邊。因此我們需要掃描中序遍歷序列,才能找到根節點的值。
如圖所示,前序遍歷序列的第一個數字1就是根節點的值。掃描中序遍歷序列,就能確定根節點的值的位置。根據中序遍歷的特點,在根節點的值1前面3個數字都是左子樹結點的值,位於1後面的數字都是右子樹結點的值。
由於中序遍歷序列中,有3個數字是左子樹結點的值,因此左子樹總共有3個左子結點。同樣,在前序遍歷的序列中,根節點後面的3個數字就是3個左子樹結點的值,再後面的所有數字都是右子樹結點的值。這樣我們就在前序遍歷和中序遍歷兩個序列中,分別找到了左右子樹對應的子序列。
既然我們已經分別找到了左、右子樹的前序遍歷序列和中序遍歷序列,我們可以用同樣的方法分別去構建左右子樹。也就是說,接下來的事情可以用遞歸的方法去完成。
算法源程序;
程序運行結果
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.