题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1, 2, 4, 7, 3, 5, 6, 8}和中序遍历序列{4, 7, 2, 1, 5, 3, 8, 6},则重建出的二叉树并输出它的头结点。
二叉树结构:
struct BinaryTreeNode {
int value;
BinaryTreeNode* left;
BinaryTreeNode* right;
};
递归求解:
BinaryTreeNode* BTNRecursion(int* preOrderStart, int* preOrderEnd, int* inOrderStart, int* inOrderEnd) {
int rootValue = *preOrderStart;
BinaryTreeNode* root = new BinaryTreeNode();
root->value = rootValue;
//防止invalid input
if (preOrderStart == preOrderEnd) {
if (inOrderStart == inOrderEnd && *preOrderStart == *inOrderStart) {
return root;
}
else {
cout << "invalid input";
return nullptr;
}
}
int* tmpInOrderStart = inOrderStart;
while (tmpInOrderStart < inOrderEnd && *tmpInOrderStart != rootValue) {
tmpInOrderStart++;
}
//找不到节点
if (*tmpInOrderStart != rootValue) {
cout << "invalid input";
return nullptr;
}
int leftLength = tmpInOrderStart - inOrderStart;
if (leftLength != 0) {
root->left = BTNRecursion(preOrderStart + 1, preOrderStart + leftLength, inOrderStart, inOrderStart + leftLength - 1);
}
if (leftLength < preOrderEnd - preOrderStart) {
root->right = BTNRecursion(preOrderStart + leftLength + 1, preOrderEnd, inOrderStart + leftLength + 1, inOrderEnd);
}
return root;
}
}
测试用例:
int main() {
/*普通测试用例*/
/*
int preOrder[] = { 1,2,4,7,3,5,6,8};
int inOrder[] = { 4,7,2,1,5,3,8,6 };
int length = 8;
*/
/*只有左节点
int preOrder[] = { 1,2,3 };
int inOrder[] = { 3,2,1 };
int length = 8;
*/
/*只有右节点
int preOrder[] = { 1,2,3 };
int inOrder[] = { 1,2,3 };
int length = 8;
*/
/*invalid input*/
int preOrder[] = { 1,2,3 };
int inOrder[] = {3,1,2 };
int length = 0;
BinaryTreeNode* root = BTNRecursion(preOrder, preOrder + length - 1, inOrder, inOrder + length - 1);
}
完整代码:
#include"pch.h"
#include<iostream>
using namespace std;
struct BinaryTreeNode {
int value;
BinaryTreeNode* left;
BinaryTreeNode* right;
};
BinaryTreeNode* BTNRecursion(int* preOrderStart, int* preOrderEnd, int* inOrderStart, int* inOrderEnd) {
int rootValue = *preOrderStart;
BinaryTreeNode* root = new BinaryTreeNode();
root->value = rootValue;
if (preOrderStart == preOrderEnd) {
if (inOrderStart == inOrderEnd && *preOrderStart == *inOrderStart) {
return root;
}
else {
cout << "invalid input";
return nullptr;
}
}
int* tmpInOrderStart = inOrderStart;
while (tmpInOrderStart < inOrderEnd && *tmpInOrderStart != rootValue) {
tmpInOrderStart++;
}
if (*tmpInOrderStart != rootValue) {
cout << "invalid input";
return nullptr;
}
int leftLength = tmpInOrderStart - inOrderStart;
if (leftLength != 0) {
root->left = BTNRecursion(preOrderStart + 1, preOrderStart + leftLength, inOrderStart, inOrderStart + leftLength - 1);
}
if (leftLength < preOrderEnd - preOrderStart) {
root->right = BTNRecursion(preOrderStart + leftLength + 1, preOrderEnd, inOrderStart + leftLength + 1, inOrderEnd);
}
return root;
}
int main() {
/*普通测试用例*/
/*
int preOrder[] = { 1,2,4,7,3,5,6,8};
int inOrder[] = { 4,7,2,1,5,3,8,6 };
int length = 8;
*/
/*只有左节点
int preOrder[] = { 1,2,3 };
int inOrder[] = { 3,2,1 };
int length = 8;
*/
/*只有右节点
int preOrder[] = { 1,2,3 };
int inOrder[] = { 1,2,3 };
int length = 8;
*/
/*invalid input*/
int preOrder[] = { 1,2,3 };
int inOrder[] = {3,1,2 };
int length = 0;
BinaryTreeNode* root = BTNRecursion(preOrder, preOrder + length - 1, inOrder, inOrder + length - 1);
}