面試題 6: 重建二叉樹

一. 題目
輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二叉樹.假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字.例如輸入前序遍歷序列{1,2,4,7,3,5,6,8}和中序編列序列{4,7,2,1,5,3,8,6},則重建出圖所示的二叉樹並輸出它的頭結點.

代碼請到我的代碼庫中下載 Point2Offer

二. 代碼

package week_3;
/**難度係數:****
 * 劍指offer: 輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二節樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。
 * 方法:遞歸
 * 測試用例:功能測試(樹的五種情況)
 * @author dingding
 * Date:2017-7-1 19:00
 * Declaration: All Rights Reserved!
 */
public class No6 {

    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();
    }

    //構造二叉樹
    private static BinaryTreeNode construct(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);
    }

    //核心代碼
    private static BinaryTreeNode constructCore(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd) {
        if (preStart>preEnd) {
            return null;
        }
        int value = preorder[preStart];
        int index = inStart;
        //在中序遍歷的數組中找根節點的位置
        while (index <=inEnd && inorder[index]!=value){
            index++;
        }

        if (index >inEnd) {
            throw new RuntimeException("Invalid input.");
        }

        //創建當前的根節點,並且爲節點賦值
        BinaryTreeNode node = new BinaryTreeNode(value);

        node.left = constructCore(preorder, preStart+1, preStart+index-inStart, inorder, inStart, index-1);
        node.right = constructCore(preorder, preStart+index-inStart+1, preEnd, inorder, index+1, inEnd);

        return node;
    }

    //重建後二叉樹以前序遍歷的方式輸出
    private static void printTree(BinaryTreeNode root){
        if (root!=null) {
            System.out.print(root.value+" ");
            printTree(root.left);
            printTree(root.right);
        }
    }
    /*====================測試用例=================*/
    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);        
    }

    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);        
    }

    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);        
    }

    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);        
    }

}

//定義一個樹
class BinaryTreeNode{
    int value;
    BinaryTreeNode left;
    BinaryTreeNode right;

    public BinaryTreeNode(int value) {
        this.value = value;
        this.left = null;
        this.right = null;
    }
}


有不妥當之處,麻煩告知:D

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