二叉搜索树的后序遍历序列


题目:输入一个整数数组,判断该数组是不是某二叉树的后序遍历结果。如果是则返回true,否则返回false。假如输入的数组的任意两个数字都互不相同。


程序代码:

#include <iostream>
#include <stding>
#include <queue>

// 函数名称:CreateBinaryTree
// 函数功能:通过二叉树的先序序列创建二叉树
// 输入参数:
//          pRoot       : 二叉树的根节点
//          strPreorder : 二叉树的先序遍历结果,空结点用‘#’表示  
// 输出参数:
//          无
// 存在问题:二叉树的结点只能是个位数字
// 函数作者:FGC
// 完成时间:2018-03-20
void CreateBinaryTree(BinaryTreeNode *&pRoot, std::string &strPreorder)
{
    pRoot = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
    if (strPreorder.size() == 0)
    {
        pRoot = NULL;
        return ;
    }

    if (strPreorder[0] == '#')
    {
        strPreorder = strPreorder.substr(1, strPreorder.size());
        pRoot = NULL;
        return ;
    }
    else
    {
        pRoot->data = strPreorder[0] - '0';
        strPreorder = strPreorder.substr(1, strPreorder.size()); 
        CreateBinaryTree(pRoot->leftChild, strPreorder);
        CreateBinaryTree(pRoot->rightChild, strPreorder);
    }
    return ;
}

// 函数名称:PrintBinaryTree
// 函数功能:先序打印二叉树
// 输入参数:
//          pRoot:二叉树根节点
// 输出参数:
//          无
// 存在问题:无
// 函数作者:FGC
// 完成时间:2018-03-20
void PrintBinaryTree(BinaryTreeNode *pRoot)
{
    if (pRoot == NULL)
    {
        return;
    }
    std::cout << pRoot->data << " ";
    PrintBinaryTree(pRoot->leftChild);
    PrintBinaryTree(pRoot->rightChild);
}

// 函数名称:IsPostOrder
// 函数功能:判断给定数列是否为二叉树的后续遍历序列
// 输入参数:
//          pRoot:二叉树根节点
//          pPostOrder:给定后序遍历序列
//          iLen:给定序列的长度
// 输出参数:
//          flagIs:true(是)
//                  false(否)
// 存在问题:无
// 函数作者:FGC
// 完成时间:2018-03-20
bool IsPostOrder(BinaryTreeNode *pRoot, int *&pPostOrder, int &iLen)
{
    bool flagIs = true;

    if (pRoot->leftChild != NULL)
    {
        flagIs = IsPostOrder(pRoot->leftChild, pPostOrder, iLen);
    }

    if (flagIs == false)
    {
        return false;
    }

    if (pRoot->rightChild != NULL)
    {
        flagIs = IsPostOrder(pRoot->rightChild, pPostOrder, iLen);
    }

    if (flagIs == false)
    {
        return false;
    }

    if (pRoot->data == *pPostOrder)
    {
        flagIs = true;
        iLen--;
        pPostOrder++;
    }
    else
    {
        flagIs = false;
    }

    return flagIs;
}

// 函数名称:IsPostOrderOfBST
// 函数功能:判断给定序列是否为搜索二叉树的后序遍历结果
// 输入参数:
//          pPostOrder:给定遍历序列
//          iLen:序列长度
// 输出参数:
//          flagIs:true(是)
//                  false(否)
// 存在问题:无
// 函数作者:FGC
// 完成时间:2018-03-20
bool IsPostOrderOfBST(int *pPostOrder, int iLen)
{
    bool flagIs = false;
    if (iLen <= 1)
    {
        return true;
    }
    else
    {
        int iLenLeft = 0;
        while (pPostOrder[iLenLeft] < pPostOrder[iLen - 1])
        {
            iLenLeft++;
        }

        int iLenRight = iLenLeft;
        while (pPostOrder[iLenRight] > pPostOrder[iLen - 1])
        {
            iLenRight++;
        }
        if (iLenRight == (iLen - 1))
        {
            flagIs = IsPostOrderOfBST(pPostOrder, iLenLeft);
            if (flagIs == true)
            {
                flagIs = IsPostOrderOfBST(pPostOrder + iLenLeft, iLen - iLenLeft - 1);
            }
        }
    }
    return flagIs;
}

int main()
{
    std::string strPreorder = "8652###7##19###";
    int *pPostOrder = (int*)malloc(sizeof(int) * 7);
    int iPostOrder[] = {5, 7, 6, 9, 11, 10, 8};
    pPostOrder[0] = 2;
    pPostOrder[1] = 5;
    pPostOrder[2] = 7;
    pPostOrder[3] = 6;
    pPostOrder[4] = 9;
    pPostOrder[5] = 1;
    pPostOrder[6] = 8;
    int len = 7;

    BinaryTreeNode *pBTRoot;
    CreateBinaryTree(pBTRoot, strPreorder);
    PrintBinaryTree(pBTRoot);
    std::cout << std::endl;

    std::cout << IsPostOrder(pBTRoot, pPostOrder, len) << std::endl;
    std::cout << "给定序列是否为搜索二叉树的后序结果:" << IsPostOrderOfBST(iPostOrder, 4) << std::endl;

    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值