題目:輸入某二叉樹的前序遍歷和中序遍歷的結果,請從新構造出該二叉樹。假設輸入的前序遍歷和中序遍歷的結果中不包含重複的數字。例如輸入的前序遍 歷序列爲{1,2,4,7,3,5,6,8}和中序遍歷爲{4,7,2,1,5,3,6,8},則重建出二叉樹並輸出它的頭結點。數組
在二叉樹的前序遍歷序列中,第一個數字老是樹的根節點的值。但在中序遍歷中,根節點的值在序列的中間,左子樹的結點的值位於根節點的值的左邊,而右子樹的結點的值位於根節點的右邊。所以咱們須要掃描中序遍歷序列,才能找到根節點的值。this
如圖所示,前序遍歷序列的第一個數字1就是根節點的值。掃描中序遍歷序列,就能肯定根節點的值的位置。根據中序遍歷的特色,在根節點的值1前面3個數字都是左子樹結點的值,位於1後面的數字都是右子樹結點的值。.net
因爲中序遍歷序列中,有3個數字是左子樹結點的值,所以左子樹總共有 3個左子結點。一樣,在前序遍歷的序列中,根節點後面的3個數字就是3個左子樹結點的值,再後面的全部數字都是右子樹結點的值。這樣咱們就在前序遍歷和中 序遍歷兩個序列中,分別找到了左右子樹對應的子序列。指針
既然咱們已經分別找到了左、右子樹的前序遍歷序列和中序遍歷序列,咱們能夠用一樣的方法分別去構建左右子樹。也就是說,接下來的事情能夠用遞歸的方法去完成。遞歸
咱們使用Java語言來實現上面的代碼:索引
package cglib;get
class BinaryTreeNode {
public int value;
public BinaryTreeNode leftNode;
public BinaryTreeNode rightNode;
public BinaryTreeNode(){
}
public BinaryTreeNode(int value){
this.value = value ;
this.leftNode = null;
this.rightNode = null;
}
}
public class List1
{
/**
*
* @param preOrder 前序遍歷數組
* @param inOrder 中序遍歷數組
* @param length 數組長度
* @return 根結點
*/
public static BinaryTreeNode Construct(int[] preOrder, int[] inOrder,int length){
if (preOrder == null || inOrder == null || length <= 0) {
System.out.println("空指針");
return null;
}
try {
return ConstructCore(preOrder, 0, preOrder.length - 1, inOrder, 0,inOrder.length - 1);
} catch (InvalidPutException e) {
e.printStackTrace();
return null;
}
}
/**
*
* @param PreOrder
* 前序遍歷序列
* @param startPreIndex
* 前序序列開始位置
* @param endPreIndex
* 前序序列結束位置
* @param InOrder
* 中序遍歷序列
* @param startInIndex
* 中序序列開始位置
* @param endInIndex
* 中序序列結束位置
* @return 根結點
* @throws InvalidPutException
*/
public static BinaryTreeNode ConstructCore(int[] preOrder,int startPreIndex, int endPreIndex,
int[] inOrder,int startInIndex, int endInIndex) throws InvalidPutException {
int rootValue = preOrder[startPreIndex]; // startPreIndex=0
System.out.println("根節點值:rootValue = " + rootValue);
BinaryTreeNode root = new BinaryTreeNode(rootValue);
// 只有一個元素
if (startPreIndex == endPreIndex) {
System.out.println("前序的開始和結束位置相等");
System.out.println("startPreIndex="+startPreIndex);
System.out.println("endPreIndex="+endPreIndex);
System.out.println("preOrder[startPreIndex]="+preOrder[startPreIndex]);
System.out.println("inOrder[startInIndex]="+inOrder[startInIndex]);
System.out.println("startInIndex="+startInIndex);
System.out.println("endInIndex="+endInIndex);
if (startInIndex == endInIndex
&& preOrder[startPreIndex] == inOrder[startInIndex]) {
System.out.println("中序開始位置等於中序結束位置,而且前序開始位置之值等於中序開始位置之值,返回root="+root);
return root;
} else {
System.out.println("拋出異常");
throw new InvalidPutException();
}
}
// 在中序遍歷中找到根結點的索引
System.out.println("在中序遍歷中找根結點的索引");
int rootInIndex = startInIndex; // startInIndex=0,根節點索引在前序組中是0
while (rootInIndex <= endInIndex && inOrder[rootInIndex] != rootValue) {
System.out.println("還沒找到,自增");
++rootInIndex;
System.out.println("rootInIndex="+rootInIndex);
}
if (rootInIndex == endInIndex && inOrder[rootInIndex] != rootValue) {
System.out.println("索引到底了,值不一樣拋出異常");
throw new InvalidPutException();
}
int leftLength = rootInIndex - startInIndex; // rootInIndex=3,序號;startInIndex=0;左子樹長度是3,有3個元素
int leftPreOrderEndIndex = startPreIndex + leftLength; //前序裏面的左子樹1->2,4,7,結束到7,索引號爲3
if (leftLength > 0) {
// 構建左子樹
root.leftNode = ConstructCore(preOrder, startPreIndex + 1,
leftPreOrderEndIndex, inOrder, startInIndex,
rootInIndex - 1); //4,7,2<-1
}
if (leftLength < endPreIndex - startPreIndex) {
// 右子樹有元素,構建右子樹
root.rightNode = ConstructCore(preOrder, leftPreOrderEndIndex + 1, //1,2,4,7,3,5,6,8 從3開始,序號是4
endPreIndex, inOrder, rootInIndex + 1, endInIndex);
} //1->5,3,8,6
return root;
}
static class InvalidPutException extends Exception {
private static final long serialVersionUID = 1L;
}
public static void printPreOrder(BinaryTreeNode root) {
if (root == null) {
return;
} else {
System.out.print(root.value + " ");
}
if (root.leftNode != null) {
printPreOrder(root.leftNode);
}
if (root.rightNode != null) {
printPreOrder(root.rightNode);
}
}
public static void main(String[] args) throws Exception{
List1 test=new List1();
int[] preOrder={1,2,4,7,3,5,6,8};
//int[] preOrder={1,2,4,7,3,5,6,9};
int[] inOrder={4,7,2,1,5,3,8,6};
printPreOrder(Construct(preOrder, inOrder, preOrder.length));
}
} io
輸出:class
根節點值:rootValue = 1
在中序遍歷中找根結點的索引
還沒找到,自增
rootInIndex=1
還沒找到,自增
rootInIndex=2
還沒找到,自增
rootInIndex=3
根節點值:rootValue = 2
在中序遍歷中找根結點的索引
還沒找到,自增
rootInIndex=1
還沒找到,自增
rootInIndex=2
根節點值:rootValue = 4
在中序遍歷中找根結點的索引
根節點值:rootValue = 7
前序的開始和結束位置相等
startPreIndex=3
endPreIndex=3
preOrder[startPreIndex]=7
inOrder[startInIndex]=7
startInIndex=1
endInIndex=1
中序開始位置等於中序結束位置,而且前序開始位置之值等於中序開始位置之值,返回root=cglib.BinaryTreeNode@139a55
根節點值:rootValue = 3
在中序遍歷中找根結點的索引
還沒找到,自增
rootInIndex=5
根節點值:rootValue = 5
前序的開始和結束位置相等
startPreIndex=5
endPreIndex=5
preOrder[startPreIndex]=5
inOrder[startInIndex]=5
startInIndex=4
endInIndex=4
中序開始位置等於中序結束位置,而且前序開始位置之值等於中序開始位置之值,返回root=cglib.BinaryTreeNode@1db9742
根節點值:rootValue = 6
在中序遍歷中找根結點的索引
還沒找到,自增
rootInIndex=7
根節點值:rootValue = 8
前序的開始和結束位置相等
startPreIndex=7
endPreIndex=7
preOrder[startPreIndex]=8
inOrder[startInIndex]=8
startInIndex=6
endInIndex=6
中序開始位置等於中序結束位置,而且前序開始位置之值等於中序開始位置之值,返回root=cglib.BinaryTreeNode@106d69c
1 2 4 7 3 5 6 8test