题目:输入一个整数数组,判断该数组是不是某二叉树的后序遍历结果。如果是则返回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;
}