根据给定先序和中序序列来重建二叉树

一、问题描述:

       输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。如果输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,15,38,6},则重建出对应的二叉树。二叉树结点的定义如下:

         struct  BinaryTreeNode

         {

                   int                          m_nValue;

                   BinaryTreeNode   *m_pLeft;

                   BinaryTreeNode   *m_pRight;

         };            


二、解决思路:

        我们可以用递归的思想来解决这个问题:因为它可以分解为许多步骤,而且每一个步骤都是算法相同,只是规模不同。

        算法:根据先序遍历的第一个数字创建根结点,接下来在中序遍历序列中找到根结点的位置,这样就能确定左右子树结点的数量。在先序遍历和中序遍历的序列中划分了左、右子树结点的值之后,我们就可以递归地去分别构建它的左右子树。

        注意:

       (1)、先序序列的遍历顺序是:根、左、右, 所以序列的第一个元素即是二叉树的根结点的值。

       (2)、中序序列的遍历顺序是:左、根、右;则根据先序序列得到的根结点的值可以区分出左右子树各有哪些结点。

       (3)、递归终点:只有根结点,没有左右子树。


三、源代码:

    #include <iostream>
    using namespace std;
     
    //树节点结构
    struct BinaryTreeNode
    {
        int                m_nValue;
        BinaryTreeNode  *m_pLeft;
        BinaryTreeNode    *m_pRight;
    };
     
    /*
     *函数功能: 根据先序和中序序列递归法构造二叉树
     *参数说明: @startPreorder: 先序序列第一个值的指针
     *            @endPreorder:    先序序列最后一个值的指针
     *            @startInorder : 中序序列第一个值的指针
     *            @endInorder :    中序序列最后一个值的指针
     *返回值:   构造出的二叉树的根结点的指针
     */
    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   //给定序列有误
            {
                return NULL;
            }
        }
     
        //在中序遍历序列中找到根结点的值
        int *rootInorder = startInorder;
        while(rootInorder <= endInorder && *rootInorder != rootValue)
        {
            rootInorder++;
        }
     
        //没找到(序列有误)
        if(rootInorder == endInorder && *rootInorder != rootValue)
        {
            return NULL;
        }
     
     
        //获取左子树结点的个数
        int leftLength = rootInorder - startInorder;
        int *leftPreorderEnd = startPreorder + leftLength;
        
        //如果有左子树
        if(leftLength > 0)
        {
            //通过左子树的先序和中序序列来构建左子树
            root->m_pLeft = ConstructCore(startPreorder + 1, leftPreorderEnd, startInorder, rootInorder - 1);
        }
     
        //如果有右子树
        if(leftLength < endPreorder- startPreorder)
        {
            //通过右子树的先序和中序序列来构建右子树
            root->m_pRight = ConstructCore(leftPreorderEnd + 1, endPreorder, rootInorder + 1, endInorder);
        }
     
        return root;
    }
     
     
    /*
     *函数功能: 根据先序和中序序列构造二叉树的函数入口
     *参数说明: @preorder: 先序序列指针
     *            @inorder:  中序序列指针
     *          @length:   二叉树的结点个数
     *返回值:   返回构造出的二叉树的根结点指针
     */
    BinaryTreeNode* Construct(int *preorder, int *inorder, int length)
    {
        if(preorder == NULL || inorder == NULL || length <= 0)
            return NULL;
     
        return ConstructCore(preorder, preorder + length - 1, inorder, inorder + length - 1);
    }
     
    //先序遍历
    void PreorderTree(BinaryTreeNode *root)
    {
        if(root != NULL)
        {
            printf("%d, ", root->m_nValue);
            PreorderTree(root->m_pLeft);
            PreorderTree(root->m_pRight);
        }
    }
     
    //中序遍历
    void InorderTree(BinaryTreeNode *root)
    {
        if(root != NULL)
        {
            InorderTree(root->m_pLeft);
            printf("%d, ", root->m_nValue);        
            InorderTree(root->m_pRight);
        }
    }
     
    //销毁二叉树
    void DestoryTree(BinaryTreeNode **root)
    {
        if(*root != NULL)
        {
            DestoryTree(&((*root)->m_pLeft));
            DestoryTree(&((*root)->m_pRight));
            delete *root;
            *root = NULL;
        }
    }
     
    int main(void)
    {
        //给定先序遍历序列和中序遍历序列
        int Preorder[] = {1, 2, 4, 7, 3, 5, 6, 8};
        int Inorder[] = {4, 7, 2, 1, 5, 3, 8, 6};
     
        BinaryTreeNode *root;
        
        //得到创建的二叉树
        root = Construct(Preorder, Inorder, sizeof(Preorder)/sizeof(Preorder[0]));
     
        printf("先序遍历结果:\n");
        PreorderTree(root);
        printf("\n");
        printf("中序遍历结果:\n");
        InorderTree(root);
        printf("\n");
     
        DestoryTree(&root);
        return 0;
    }




四、运行截图:

                  


文章源自剑指Offer!


---------------------
版权声明:本文为CSDN博主「迷雾江湖」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/SunXiWang/article/details/78975755

posted @ 2019-08-06 16:30  天涯海角路  阅读(395)  评论(0)    收藏  举报