劍指offer之重建二叉樹

在這裏插入圖片描述

	/**
     * 二叉樹節點類
     */
    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||preorder.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 && value!=inorder[index]){
    		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+1,ps+index-is,inorder,is,index-1);
        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);
    }

    // 輸入空指針
    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);
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章