天天看點

根據前序周遊、中序周遊重建二叉樹

本題來源于《劍指offer》55頁面試題6:重建二叉樹。

題目如下:

輸入某二叉樹的前序周遊和中序周遊結果,請重建出該二叉樹。假設輸入的前序周遊和中序周遊中都不含有重複的數字,二叉樹節點定義略。

代碼如下:

#include<iostream>
using namespace std;
struct BinaryTreeNode
{
	BinaryTreeNode *m_pLeft;
	BinaryTreeNode *m_pRight;
	int m_nValue;
};
BinaryTreeNode* ConstructCore(int *startPreOrder,int *endPreOrder,int *startInOrder,int *endInOrder);
BinaryTreeNode* Construct(int *startPreOrder,int *startInOrder,int length)
{
	if(startPreOrder==NULL||startInOrder==NULL||length==0)
		return NULL;
	return ConstructCore(startPreOrder,startPreOrder+length-1,startInOrder,startInOrder+length-1);
}

BinaryTreeNode* ConstructCore(int *startPreOrder,int *endPreOrder,int *startInOrder,int *endInOrder)
{
	int rootValue=startPreOrder[0];
	BinaryTreeNode* root=new BinaryTreeNode();
	root->m_nValue=rootValue;
	root->m_pLeft=root->m_pRight=NULL;
	if(startPreOrder==endPreOrder)
	{
		if((startInOrder==endInOrder)&&(*startPreOrder==*startInOrder))
		{
			return root;
		}
	}
	else
		throw::std::exception("Invalid input.");

	int *rootInOrder=startInOrder;
	while((*rootInOrder!=rootValue)&&(rootInOrder<=endInOrder))
		rootInOrder++;
	if((rootInOrder==endInOrder)&&(*rootInOrder!=rootValue))
		throw::std::exception("Invalid input.");

	int leftLength=rootInOrder-startInOrder;
	if(leftLength>0)
	{
		root->m_pLeft=ConstructCore(startPreOrder+1,startPreOrder+leftLength,startInOrder,rootInOrder-1);
	}
	if(leftLength<endPreOrder-startPreOrder)
	{
		root->m_pRight=ConstructCore(startPreOrder+leftLength+1,endPreOrder,rootInOrder+1,endInOrder);
	}

	return root;
}
           
// ====================測試代碼====================
// ====================測試代碼很多,這是直接使用作者提供的測試代碼======================
void Test(char* testName, int* preorder, int* inorder, int length)
{
    if(testName != NULL)
        printf("%s begins:\n", testName);

    printf("The preorder sequence is: ");
    for(int i = 0; i < length; ++ i)
        printf("%d ", preorder[i]);
    printf("\n");

    printf("The inorder sequence is: ");
    for(int i = 0; i < length; ++ i)
        printf("%d ", inorder[i]);
    printf("\n");

    try
    {
        BinaryTreeNode* root = Construct(preorder, inorder, length);
    }
    catch(std::exception& exception)
    {
        printf("Invalid Input.\n");
    }
}

// 普通二叉樹
//              1
//           /     \
//          2       3  
//         /       / \
//        4       5   6
//         \         /
//          7       8
void Test1()
{
    const int length = 8;
    int preorder[length] = {1, 2, 4, 7, 3, 5, 6, 8};
    int inorder[length] = {4, 7, 2, 1, 5, 3, 8, 6};

    Test("Test1", preorder, inorder, length);
}

// 所有結點都沒有右子結點
//            1
//           / 
//          2   
//         / 
//        3 
//       /
//      4
//     /
//    5
void Test2()
{
    const int length = 5;
    int preorder[length] = {1, 2, 3, 4, 5};
    int inorder[length] = {5, 4, 3, 2, 1};

    Test("Test2", preorder, inorder, length);
}

// 所有結點都沒有左子結點
//            1
//             \ 
//              2   
//               \ 
//                3 
//                 \
//                  4
//                   \
//                    5
void Test3()
{
    const int length = 5;
    int preorder[length] = {1, 2, 3, 4, 5};
    int inorder[length] = {1, 2, 3, 4, 5};

    Test("Test3", preorder, inorder, length);
}

// 樹中隻有一個結點
void Test4()
{
    const int length = 1;
    int preorder[length] = {1};
    int inorder[length] = {1};

    Test("Test4", preorder, inorder, length);
}

// 完全二叉樹
//              1
//           /     \
//          2       3  
//         / \     / \
//        4   5   6   7
void Test5()
{
    const int length = 7;
    int preorder[length] = {1, 2, 4, 5, 3, 6, 7};
    int inorder[length] = {4, 2, 5, 1, 6, 3, 7};

    Test("Test5", preorder, inorder, length);
}

// 輸入空指針
void Test6()
{
    Test("Test6", NULL, NULL, 0);
}

// 輸入的兩個序列不比對
void Test7()
{
    const int length = 7;
    int preorder[length] = {1, 2, 4, 5, 3, 6, 7};
    int inorder[length] = {4, 2, 8, 1, 6, 3, 7};

    Test("Test7: for unmatched input", preorder, inorder, length);
}

int main()
{
    Test1();
    Test2();
    Test3();
    Test4();
    Test5();
    Test6();
    Test7();

    return 0;
}
           

運作結果如下:

根據前序周遊、中序周遊重建二叉樹