题目:
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
Example:
例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
问题解析:
树存在先序遍历、中序遍历、后序遍历三种遍历方式。
- 先序遍历和中序遍历重建数组;
- 后序遍历和中序遍历重建数组。
链接:
剑指Offer(第2版):P62
LeetCode:
思路标签:
算法:遍历、递归
数据结构:二叉搜索树
解答:
1. C++
- 先序遍历序列的第一个数字是根结点值;
- 中序遍历中根结点的左边序列是左子树,右边序列是右子树。
- 解题是特别还需要处理不符合条件的情况:序列为空的情况;两序列元素个数不同的情况;两序列中元素不相同的情况。
先序遍历和中序遍历:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* reConstructBinaryTree(vector<int> pre, vector<int> vin) {
if (pre.size() == 0 || vin.size() == 0)
return nullptr;
int *preStart = &pre[0];
int *preEnd = &pre[pre.size() - 1];
int *vinStart = &vin[0];
int *vinEnd = &vin[vin.size() - 1];
return ConstructCore(preStart, preEnd, vinStart, vinEnd);
}
TreeNode* ConstructCore(int* preStart, int* preEnd, int* vinStart, int* vinEnd)
{
int rootValue = preStart[0];
TreeNode* root = new TreeNode(preStart[0]);
root->val = rootValue;
root->left = nullptr;
root->right = nullptr;
if (preStart == preEnd)
{
if (vinStart == vinEnd && *preStart == *vinStart)
return root;
//else
//throw std::exception("Invalid input");
}
int *rootVin = vinStart;
while (rootVin < vinEnd && *rootVin != rootValue)
++rootVin;
//if(rootVin == vinEnd && *rootVin != rootValue)
//throw std::exception("Invalid input");
int leftLength = rootVin - vinStart;
int* preLeftEnd = preStart + leftLength;
if (leftLength > 0)
{
//构建左子树
root->left = ConstructCore(preStart + 1, preLeftEnd, vinStart, rootVin - 1);
}
if (leftLength < preEnd - preStart)
{
//构建右子树
root->right = ConstructCore(preLeftEnd + 1, preEnd, rootVin + 1, vinEnd);
}
return root;
}
void preorder(TreeNode* root) {
if (root) {
cout << root->val;
preorder(root->left);
preorder(root->right);
}
cout << endl;
}
};
中序遍历和后序遍历:
class Solution {
public:
TreeNode* reConstructBinaryTree(vector<int> post, vector<int> in){
int postLength = post.size();
int inLength = in.size();
if (postLength == 0 || inLength == 0) return nullptr;
int* postStart = &post[0];
int* postEnd = &post[postLength - 1];
int* inStart = &in[0];
int* inEnd = &in[inLength - 1];
return ConstructCore(postStart, postEnd, inStart, inEnd);
}
TreeNode* ConstructCore(int* postStart, int* postEnd, int* inStart, int* inEnd)
{
int rootValue = *postEnd;
TreeNode* root = new TreeNode(rootValue);
if (postStart == postEnd) {
if (inStart == inEnd && *inStart == *postStart)
return root;
else
throw std::exception("Invalid value!");
}
int* inRoot = inStart;
while (inRoot < inEnd && *inRoot != rootValue)
++inRoot;
if (inRoot == inEnd && *inRoot != rootValue)
throw std::exception("Invalid value!");
int rightLength = inEnd - inRoot;
int* rightPostStart = postEnd - rightLength;
if (rightLength > 0) {
root->right = ConstructCore(rightPostStart, postEnd - 1, inRoot + 1, inEnd);
}
if (rightLength < postEnd - postStart) {
root->left = ConstructCore(postStart, rightPostStart - 1, inStart, inRoot - 1);
}
return root;
}
};
2. Java
先序遍历和中序遍历:
http://blog.csdn.net/koala_tree/article/details/78548628
中序遍历和后序遍历: