就以infix-order和prefix-order举例,尝试还原后序遍历序列吧,其他情况都是类似的
这个问题一般分两种:
一种是还原出一个序列(sequence + sequence -> sequence)
另外一种是还原出一颗二叉树(sequence + sequence -> binary tree)
代码分别如下:写的比较规范,可作为模板使用,仅作为参考
void build(
std::vector<int> const & preorder,
std::vector<int> const & inorder,
std::vector<int> & postorder,
int preorder_first,
int inorder_first,
int postorder_last,
int current_length){
if(! current_length) return;
if(1 == current_length)
return (void)(postorder[postorder_last - 1] = preorder[preorder_first]);
postorder[postorder_last - 1] = preorder[preorder_first];
int p { inorder_first };
while(! (inorder[p] == preorder[preorder_first])) ++ p;
build(preorder, inorder, postorder, preorder_first + 1, preorder_first + 1, p, p - preorder_first);
build(preorder, inorder, postorder, preorder_first + p - inorder_first + 1, p + 1, postorder_last, current_length - p - 1);
}
std::vector<int> build(std::vector<int> const & preorder, std::vector<int> const & inorder){
auto length { static_cast<int>(preorder.size()) };
std::vector<int> postorder(length);
postorder.reserve(length << 1 | 1);
build(preorder, inorder, postorder, 0, 0, length, length);
return postorder;
}
void build(
std::vector<int> const & preorder,
std::vector<int> const & inorder,
int preorder_first,
int preorder_last,
int inorder_first,
int inorder_last,
Node ** const Ot){
(void) std::exchange( *Ot, new Node {} );
(*Ot)->value = preorder[preorder_first];
auto m { inorder_first };
while(! (m == inorder_last) && ! (preorder[preorder_first] == inorder[m])) ++ m;
if(m == inorder_last - 1) return (void) std::exchange((*Ot) -> right, nullptr);
if(m == inorder_first) return (void) std::exchange((*Ot) -> left, nullptr);
auto length_left { m - inorder_first };
build(preorder, inorder, preorder_first + 1, preorder_first + 1 + length_left, inorder_first, m, &((*Ot) -> left));
build(preorder, inorder, preorder_first + 1 + length_left, preorder_last, m + 1, inorder_last, &((*Ot) -> right));
}
template <typename = void>
Node *build(std::vector<int> const & preorder, std::vector<int> const & inorder){
auto length { static_cast<int>(preorder.size()) };
std::vector<int> postorder(length);
postorder.reserve(length << 1 | 1);
Node *root {nullptr};
build(preorder, inorder, 0, length, 0, length, &root);
return root;
}