【劍指Offer學習】【面試題6 :重建二叉樹】,劍指offer
題目:輸入某二叉樹的前序走訪和中序遍曆的結果,請重建出該二叉樹。假設輸入的前序走訪和中序遍曆的結果中都不含重複的數字。例如:前序走訪序列{ 1, 2, 4, 7, 3, 5, 6, 8}和中序遍曆序列{4, 7, 2, 1, 5, 3, 8,6},重建出所示的二叉樹並輸出它的頭結點。
代碼如下:
public class Test06 { /** * 二叉樹節點類 */ public static class BinaryTreeNode { int value; BinaryTreeNode left; BinaryTreeNode right; } /** * 輸入某二叉樹的前序走訪和中序遍曆的結果,請重建出該二節樹。假設輸入的前序走訪和中序遍曆的結果中都不含重複的數字。 * * @param preorder 前序走訪 * @param inorder 中序遍曆 * @return 樹的根結點 */ public static BinaryTreeNode construct(int[] preorder, int[] inorder) { // 輸入的合法性判斷,兩個數組都不可為空,並且都有資料,而且資料的數目相同 if (preorder == null || inorder == null || preorder.length != inorder.length || inorder.length < 1) { return null; } return construct(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1); } /** * 輸入某二叉樹的前序走訪和中序遍曆的結果,請重建出該二節樹。假設輸入的前序走訪和中序遍曆的結果中都不含重複的數字。 * * @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 + 1, ps + index - is, inorder, is, index - 1); // 遞迴構建當前根結點的右子樹,右子樹的元素個數:ie-index個 // 右子樹對應的前序走訪的位置在[ps+index-is+1, pe] // 右子樹對應的中序遍曆的位置在[index+1, ie] node.right = construct(preorder, ps + index - is + 1, pe, inorder, index + 1, 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() { int[] preorder = {1, 2, 4, 7, 3, 5, 6, 8}; int[] inorder = {4, 7, 2, 1, 5, 3, 8, 6}; BinaryTreeNode root = construct(preorder, inorder); printTree(root); } // 所有結點都沒有右子結點 // 1 // / // 2 // / // 3 // / // 4 // / // 5 private static void test2() { int[] preorder = {1, 2, 3, 4, 5}; int[] inorder = {5, 4, 3, 2, 1}; BinaryTreeNode root = construct(preorder, inorder); printTree(root); } // 所有結點都沒有左子結點 // 1 // \ // 2 // \ // 3 // \ // 4 // \ // 5 private static void test3() { int[] preorder = {1, 2, 3, 4, 5}; int[] inorder = {1, 2, 3, 4, 5}; BinaryTreeNode root = construct(preorder, inorder); printTree(root); } // 樹中只有一個結點 private static void test4() { int[] preorder = {1}; int[] inorder = {1}; BinaryTreeNode root = construct(preorder, inorder); printTree(root); } // 完全二叉樹 // 1 // / \ // 2 3 // / \ / \ // 4 5 6 7 private static void test5() { int[] preorder = {1, 2, 4, 5, 3, 6, 7}; int[] inorder = {4, 2, 5, 1, 6, 3, 7}; BinaryTreeNode root = construct(preorder, inorder); printTree(root); } // 輸入null 指標 private static void test6() { construct(null, null); } // 輸入的兩個序列不匹配 private static void test7() { int[] preorder = {1, 2, 4, 5, 3, 6, 7}; int[] inorder = {4, 2, 8, 1, 6, 3, 7}; 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(); }}
運行結果: