码迷,mamicode.com
首页 > 其他好文 > 详细

题目六:重建二叉树

时间:2019-07-28 13:43:50      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:后序遍历   UNC   putchar   restart   har   pad   ||   pos   ldb   

///////////////////////////////////////////////////////////////////////////////////////
// // 9.题目六:重建二叉树
// 题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出改二叉树


struct BinaryTreeNode
{
    int m_iValue;
    BinaryTreeNode* m_pLeft;
    BinaryTreeNode* m_pRight;

    BinaryTreeNode(int iValue = 0, BinaryTreeNode* pLeft = NULL, BinaryTreeNode* pRight = NULL)
        :m_iValue(iValue), m_pLeft(pLeft), m_pRight(pRight)
    {

    }
};

BinaryTreeNode* RebuildTree(int* pPreStart, int* pPreEnd, int* pInStart, int* pInEnd)
{
    if (pPreStart > pPreEnd || pInStart > pInEnd)
    {
        return NULL;
    }

    // 1.建立根节点(前序遍历第一个元素)
    BinaryTreeNode* pRoot = new BinaryTreeNode(*pPreStart);
    if (!pRoot)
    {
        return NULL;
    }

    // 2.找到中序根节点
    int* p = pInStart;
    while (p <= pInEnd && *p != pRoot->m_iValue)
    {
        p++;
    }

    int iLeftLength = p - pInStart;
    int* pLeftPreEnd = pPreStart + iLeftLength;

    // 3.构建左子树
    if (iLeftLength > 0)
    {
        pRoot->m_pLeft = RebuildTree(pPreStart + 1, pLeftPreEnd, pInStart, p - 1);
    }

    // 4.构建右子树
    if (iLeftLength < (pPreEnd - pPreStart))
    {
        pRoot->m_pRight = RebuildTree(pLeftPreEnd + 1, pPreEnd, p + 1, pInEnd);
    }

    return pRoot;
}

BinaryTreeNode* RebuildBinaryTree(int* piPreOrderArray, int* piInorderArray, int iLength)
{
    if (NULL == piPreOrderArray || NULL == piInorderArray || iLength <= 0)
    {
        return NULL;
    }

    return RebuildTree(piPreOrderArray, piPreOrderArray + iLength - 1, piInorderArray, piInorderArray + iLength -1);
}

void DestroyTree(BinaryTreeNode* pTree)
{
    if (pTree)
    {
        // 释放左子树
        DestroyTree(pTree->m_pLeft);

        // 释放右子树
        DestroyTree(pTree->m_pRight);

        delete pTree;
        pTree = NULL;
    }
}

// 后序遍历
void PostTraversal(BinaryTreeNode* pNode)
{
    if (pNode)
    {
        PostTraversal(pNode->m_pLeft);
        PostTraversal(pNode->m_pRight);
        printf("%02d -> ", pNode->m_iValue);
    }
}

void RebuildBinaryTreeTestFunc()
{
    cout << "\n\n --------------- RebuildBinaryTreeTestFunc Start -------------->" << endl;
    const int MAX_TREE_NODE_COUNT = 8;

    //前序遍历序列(中 -> 左 -> 右)
    int aiPreOrderArray[MAX_TREE_NODE_COUNT] = {1, 2, 4, 7, 3, 5, 6, 8};

    //中序遍历序列(左 -> 中 -> 右)
    int aiInorderArray[MAX_TREE_NODE_COUNT] = {4, 7, 2, 1, 5, 3, 8, 6};

    //后序遍历序列(左 -> 右 -> 中)
    int aiPostArray[MAX_TREE_NODE_COUNT] = {7, 4, 2, 5, 8, 6, 3, 1};

    BinaryTreeNode* pTree1 = RebuildBinaryTree(aiPreOrderArray, aiInorderArray, MAX_TREE_NODE_COUNT);
    if (pTree1)
    {
        // 后序遍历
        PostTraversal(pTree1);
        putchar(10);

        // 施法资源
        DestroyTree(pTree1);
    }

    cout << "\n\n --------------- RebuildBinaryTreeTestFunc End -------------->" << endl;

}

题目六:重建二叉树

标签:后序遍历   UNC   putchar   restart   har   pad   ||   pos   ldb   

原文地址:https://www.cnblogs.com/yzdai/p/11258611.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!