// 面试题:重建二叉树
// 题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输
// 入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,
// 2, 4, 7, 3, 5, 6, 8}和中序遍历序列{4, 7, 2, 1, 5, 3, 8, 6},则重建出
// 图2.6所示的二叉树并输出它的头结点。
1.递归法实现 (输入前序和中序遍历的结果)
大神法递归(本来用java写的,我修改后结果对)(把结构体对象有的变成指针)
int pre[]={1,2,4,7,3,5,6,8};
int in[]={4,7,2,1,5,3,8,6};
//前序遍历{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6}
TreeNode * reConstructBinaryTree1(int pre[],int startPre,int endPre,int in[],int startIn,int endIn)
{
if(startPre>endPre||startIn>endIn)//异常
return NULL;
TreeNode * root=new TreeNode(pre[startPre]);
for(int i=startIn;i<=endIn;i++)
if(in[i]==pre[startPre])
{ //注意这个地方 (i-startin) 是偏移量 两个数组不一定都从下标0开始 i=startin i++ 嘛
root->left=reConstructBinaryTree1(pre,startPre+1,startPre+i-startIn,in,startIn,i-1); //每次传进去的数组名不变化 指针变化
root->right=reConstructBinaryTree1(pre,i-startIn+startPre+1,endPre,in,i+1,endIn); //i已经是从startin开始的了
break;
}
return root;
}
TreeNode * reConstructBinaryTree(int pre [],int in [] ,int length)
{
TreeNode * root=reConstructBinaryTree1(pre,0,length-1,in,0,length-1);//就是调用这个函数
return root;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
TreeNode * node;
node=reConstructBinaryTree(pre,in,8);
cout<<node->val<<endl;
return a.exec();
}
2.递归方式(课本的方式)
#include <iostream>
#include "BinaryTree.h"
using namespace std;
BinaryTreeNode* ConstructCore(int* startPreorder, int* endPreorder, int* startInorder, int* endInorder);
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);
}
BinaryTreeNode* ConstructCore(int* startPreorder, int* endPreorder,int* startInorder, int* endInorder)//注意传入的是地址
{
// 前序遍历序列的第一个数字是根结点的值
BinaryTreeNode* root = CreateBinaryTreeNode(startPreorder[0]);//建立根节点
if (startPreorder == endPreorder)//如果这个树只有根节点
{
if (startInorder == endInorder && *startPreorder == *startInorder)
return root;
else //注意判断输入是否真的是对的
throw exception("Invalid input.");
}
// 在中序遍历中找到根结点的值
int* rootInorder = startInorder;
while (rootInorder <= endInorder && *rootInorder != startPreorder[0])
++rootInorder;
if (rootInorder == endInorder && *rootInorder != startPreorder[0])//如果中序遍历中没有根节点,就抛出异常
throw exception("Invalid input.");
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 Test(const char* testName, int* preorder, int* inorder, int length)
{
if (testName != NULL)
cout << testName << " begins:
";
cout << "The preorder sequence is: ";
for (int i = 0; i < length; ++i)
cout << preorder[i];
cout << endl;
cout << "The inorder sequence is: ";
for (int i = 0; i < length; ++i)
cout << inorder[i];
cout << endl;
try
{
BinaryTreeNode* root = Construct(preorder, inorder, length);
PrintTree(root);
DestroyTree(root);
}
catch (exception& exception)
{
cout << "Invalid Input.
";
}
}
// 普通二叉树
// 1
// /
// 2 3
// / /
// 4 5 6
// /
// 7 8
void Test1()
{
const int length = 8;
int preorder[length] = { 1, 2, 4, 7, 3, 5, 6, 8 };
int inorder[length] = { 4, 7, 2, 1, 5, 3, 8, 6 };
Test("Test1", preorder, inorder, length);
}
// 所有结点都没有右子结点
// 1
// /
// 2
// /
// 3
// /
// 4
// /
// 5
void Test2()
{
const int length = 5;
int preorder[length] = { 1, 2, 3, 4, 5 };
int inorder[length] = { 5, 4, 3, 2, 1 };
Test("Test2", preorder, inorder, length);
}
// 所有结点都没有左子结点
// 1
//
// 2
//
// 3
//
// 4
//
// 5
void Test3()
{
const int length = 5;
int preorder[length] = { 1, 2, 3, 4, 5 };
int inorder[length] = { 1, 2, 3, 4, 5 };
Test("Test3", preorder, inorder, length);
}
// 树中只有一个结点
void Test4()
{
const int length = 1;
int preorder[length] = { 1 };
int inorder[length] = { 1 };
Test("Test4", preorder, inorder, length);
}
// 完全二叉树
// 1
// /
// 2 3
// / /
// 4 5 6 7
void Test5()
{
const int length = 7;
int preorder[length] = { 1, 2, 4, 5, 3, 6, 7 };
int inorder[length] = { 4, 2, 5, 1, 6, 3, 7 };
Test("Test5", preorder, inorder, length);
}
// 输入空指针
void Test6()
{
Test("Test6", NULL, NULL, 0);
}
// 输入的两个序列不匹配
void Test7()
{
const int length = 7;
int preorder[length] = { 1, 2, 4, 5, 3, 6, 7 };
int inorder[length] = { 4, 2, 8, 1, 6, 3, 7 };
Test("Test7: for unmatched input", preorder, inorder, length);
}
int main(int argc, char* argv[])
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
Test7();
system("pause");
}