题目描述
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。
假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
输入
前序遍历序列{1,2,4,7,3,5,6,8}
中序遍历序列{4,7,2,1,5,3,8,6}
则重建二叉树并返回。
这道题还是比较简单的,我们知道
递归思想
#include <iostream>
#include <vector>
using namespace std;
// 调试开关
#define __tmain main
#ifdef __tmain
#define debug cout
#else
#define debug 0 && cout
#endif // __tmain
#ifdef __tmain
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x)
: val(x), left(NULL), right(NULL)
{
}
//
static void PreOrder(TreeNode *root)
{
if(root == NULL)
{
return;
}
cout <<root->val;
PreOrder(root->left);
PreOrder(root->right);
}
static void InOrder(TreeNode *root)
{
if(root == NULL)
{
return;
}
InOrder(root->left);
cout <<root->val;
InOrder(root->right);
}
};
#endif // __tmain
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution
{
public:
struct TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> in)
{
// 前序遍历的长度跟中序遍历的长度应该相同
if(pre.size( ) != in.size( ))
{
debug <<"the length of PRE and IN should be smae" <<endl;
return NULL;
}
// 长度不能为0
int size = pre.size( );
if(size == 0)
{
debug <<"it's a NULL tree(length = 0)" <<endl;
return NULL;
}
int length = pre.size( );
debug <<"the length of your tree = " <<length <<endl;
int value = pre[0]; // 前序遍历的第一个结点是根节点
TreeNode *root = new TreeNode(value);
debug <<"the root is" <<root->val <<endl;
// 在中序遍历中查找到根的位置
int rootIndex = 0;
for(rootIndex = 0; rootIndex < length; rootIndex++)
{
if(in[rootIndex] == value)
{
debug <<"find the root at " <<rootIndex <<" in IN" <<endl;
break;
}
}
if(rootIndex >= length)
{
debug <<"can't find root (value = " <<value <<") in IN" <<endl;
return NULL;
}
/// 区分左子树和右子树
/// 中序遍历中, 根左边的就是左子数, 右边的就是右子树
/// 前序遍历中, 根后面是先遍历左子树, 然后是右子树
/// 首先确定左右子树的长度, 从中序遍历in中确定
int leftLength = rootIndex;
int rightLength = length - 1 - rootIndex;
debug <<"left length = " <<leftLength <<", rightLength = " <<rightLength <<endl;
vector<int> preLeft(leftLength), inLeft(leftLength);
vector<int> preRight(rightLength), inRight(rightLength);
for(int i = 0; i < length; i++)
{
if(i < rootIndex)
{
// 前序遍历的第一个是根节点, 根后面的(leftLegnth = rootIndex) - 1个节点是左子树, 因此是i+1
preLeft[i] = pre[i + 1];
// 中序遍历前(leftLength = rootIndex) - 1个节点是左子树, 第rootIndex个节点是根
inLeft[i] = in[i];
debug <<preLeft[i] <<inLeft[i] <<" ";
}
else if(i > rootIndex)
{
// 前序遍历的第一个是根节点, 根后面的(leftLegnth = rootIndex) - 1个节点是左子树, 后面是右子树
preRight[i - rootIndex - 1] = pre[i];
// 中序遍历前(leftLength = rootIndex) - 1个节点是左子树, 第rootIndex个节点是根, 然后是右子树
inRight[i - rootIndex - 1] = in[i];
debug <<preRight[i - rootIndex - 1] <<inRight[i - rootIndex - 1] <<" ";
}
}
debug <<endl <<"the left tree" <<endl;
for(int i = 0; i < leftLength; i++)
{
debug <<preLeft[i] <<inLeft[i] <<" ";
}
debug <<endl;
debug <<"the right tree" <<endl;
for(int i = 0; i < rightLength; i++)
{
debug <<preRight[i] <<inRight[i] <<" ";
}
debug <<endl;
root->left = reConstructBinaryTree(preLeft, inLeft);
root->right = reConstructBinaryTree(preRight, inRight);
return root;
}
};
int __tmain( )
{
int pre[] = { 1, 2, 4, 7, 3, 5, 6, 8 };
int in[] = { 4, 7, 2, 1, 5, 3, 8, 6 };
vector<int> preOrder(pre, pre + 8);
vector<int> inOrder( in, in + 8);
Solution solu;
TreeNode *root = solu.reConstructBinaryTree(preOrder, inOrder);
cout <<"PreOrder";
TreeNode::PreOrder(root);
cout <<endl;
cout <<"InOrder ";
TreeNode::InOrder(root);
cout <<endl;
return 0;
}