- 题目描述:
-
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并输出它的后序遍历序列。

- 输入:
-
输入可能包含多个测试样例,对于每个测试案例,
输入的第一行为一个整数n(1<=n<=1000):代表二叉树的节点个数。
输入的第二行包括n个整数(其中每个元素a的范围为(1<=a<=1000)):代表二叉树的前序遍历序列。
输入的第三行包括n个整数(其中每个元素a的范围为(1<=a<=1000)):代表二叉树的中序遍历序列。
- 输出:
-
对应每个测试案例,输出一行:
如果题目中所给的前序和中序遍历序列能构成一棵二叉树,则输出n个整数,代表二叉树的后序遍历序列,每个元素后面都有空格。
如果题目中所给的前序和中序遍历序列不能构成一棵二叉树,则输出”No”。
- 样例输入:
-
81 2 4 7 3 5 6 84 7 2 1 5 3 8 681 2 4 7 3 5 6 84 1 2 7 5 3 8 6
- 样例输出:
-
7 4 2 5 8 6 3 1 No
采用递归的方式重构二叉树,关键是要考虑到一些特殊情况,比如:只有根节点的二叉树、只有左子树或只有右子树的二叉树以及二叉树根节点为NULL、前序中序序列不匹配导致不能重构二叉树等。
AC代码如下(一直在如何实现判断能否重构二叉树的地方徘徊,在九度论坛里大致看了下,借鉴了下各位前辈的思路:定义一个全局bool变量,用来跟踪判断能够重构):
#include<stdio.h>
#include<stdlib.h>
struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
BinaryTreeNode * ConstructCore(int*,int*,int*,int *);
bool canRebuild;//用来标识是否能重构二叉树
/*
preorder为前序遍历数组,inorder为中序遍历数组,length为数组长度
*/
BinaryTreeNode* Construct(int* preorder,int* inorder,int length)
{
if(preorder==NULL||inorder==NULL||length<=0)
{
canRebuild=false;
return NULL;
}
int i;
for(i=0;i<length;i++)
if(preorder[0] == inorder[i])
break;
//如果遍历inv结束都没有找到与pre[0]相等的值,则不能重构二叉树
if(i >= length)
{
canRebuild = false;
return NULL;
}
return ConstructCore(preorder,preorder+length-1,inorder,inorder+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
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;
}
void BehTraverse(BinaryTreeNode* pTree)
{
if(pTree != NULL)
{
if(pTree->m_pLeft!= NULL)
BehTraverse(pTree->m_pLeft);
if(pTree->m_pRight != NULL)
BehTraverse(pTree->m_pRight);
printf("%d ",pTree->m_nValue);
}
}
void DestroyTree(BinaryTreeNode* pTree)
{
if(pTree)
{
if(pTree->m_pLeft)
DestroyTree(pTree->m_pLeft);
if(pTree->m_pRight)
DestroyTree(pTree->m_pRight);
free(pTree);
pTree = NULL;
}
}
int main()
{
int length;
BinaryTreeNode* pTree=NULL;
while(scanf("%d",&length)!=EOF)
{
int* preorder = (int*)malloc(length*sizeof(int));
int* inorder = (int*)malloc(length*sizeof(int));
int i;
for(i=0;i<length;i++)
scanf("%d",preorder+i);
for(i=0;i<length;i++)
scanf("%d",inorder+i);
canRebuild = true;
BinaryTreeNode* root = Construct(preorder,inorder,length);
printf("%d\t",root->m_nValue);
}
} 结果:
本文介绍了一种使用前序和中序遍历序列重构二叉树的方法,并通过递归实现了这一过程。同时,文章提供了完整的C++代码示例,展示了如何判断给定序列是否能构成有效的二叉树。

2642

被折叠的 条评论
为什么被折叠?



