给定前序遍历和中序遍历的顺序输出, 构造二叉树;
e.g.
前序: {4, 3, 7, 5, 6}
中序: {3, 4, 5, 7, 6}
递归解法
- 前序的第一个元素即为binary tree的root, 而中序输出中, 在达到root之前的都为左子树, root之后的为右子树; 利用此性质, 可以在中序中遍历至前序的第一个元素, 找到左子树的个数j;
- 利用此个数j, 可以在前序中找到左子树, 右子树的范围;
- 然后分别以左子树, 右子树为序列, 缩小索引的范围, 递归求解;
注意,递归函数的参数以及返回值的设计一直是需要特别注意的; 参数是往每一层递归递进时需要传入的变量, 返回值则是在本层递归完毕之后需要返回告知上层的值; 在之前的LCA的文章中, 参数恰好是可以被忽略了; 而本题的参数则十分重要, 从而也决定了递归函数的function prototype应该是什么样子; 同时, 对于非递归的实现,这些参数/返回值也必须要考虑进来;
本题的参数是整个序列的某一个子序列部分, 而子序列也可以被当做完整的序列处理; 递归树的出口条件是某个序列的start和end的索引位置相同;
#include <iostream>
#include <vector>
struct node {
int value;
struct node* left;
struct node* right;
node(int val):
value(val), left(nullptr), right(nullptr) {}
};
// {4, 3, 7, 5, 6}
// {3, 4, 5, 7, 6}
struct node* buildBinaryTree(const std::vector<int>& preorder, int pre_start, int pre_end, const std::vector<int>& inorder, int in_start, int in_end) {
struct node* root = new node(preorder[pre_start]);
if (pre_end - pre_start == 0) {
return root;
}
int j = -1;
while (inorder[in_start + ++j] != preorder[pre_start]) ;
root->left = buildBinaryTree(preorder, pre_start + 1, pre_start + j, inorder, in_start, in_start + j);
root->right = buildBinaryTree(preorder, pre_start + j + 1, pre_end, inorder, in_start + j + 1, in_end);
return root;
}
void inorder_print(struct node* root) {
if (root == nullptr) return;
inorder_print(root->left);
std::cout << root->value << std::endl;
inorder_print(root->right);
}
int main() {
std::vector<int> pre_order = {4, 3, 7, 5, 6};
std::vector<int> in_order = {3, 4, 5, 7, 6};
struct node* root = buildBinaryTree(pre_order, 0, pre_order.size() - 1,
in_order, 0, in_order.size() - 1);
inorder_print(root);
}
非递归解法
实现非递归的写法; 观察递归的实现, 发现此题实质也是一个类似前序周游二叉树,因此非递归实现的循环的套路基本一致;
对于struct node结构, 需要增加每层的节点的参数pre_start_, pre_end_, etc. 另外还需要记住每一个节点的相关的左子树的长度left_tree_len, 只是为了压栈之前计算下一层子树的的索引;
#include <iostream>
#include <stack>
#include <vector>
// {4, 3, 7, 5, 6}
// {3, 4, 5, 7, 6}
enum Phase { INIT, LEFT, RIGHT };
struct node {
int value;
struct node* left;
struct node* right;
Phase phase;
int pre_start_, pre_end_;
int in_start_, in_end_;
int left_tree_len;
node(int val, int pre_start, int pre_end, int in_start, int in_end):
value(val), left(nullptr), right(nullptr), phase(INIT),
pre_start_(pre_start), pre_end_(pre_end),
in_start_(in_start), in_end_(in_end) {}
};
struct node* buildBinaryTree(const std::vector<int>& preorder, int pre_start, int pre_end, const std::vector<int>& inorder, int in_start, int in_end) {
std::stack<node*> stack;
struct node* root = new node(preorder[pre_start], pre_start, pre_end, in_start, in_end);
stack.push(root);
while (stack.size() > 0) {
struct node* top = stack.top();
if (top->pre_end_ - top->pre_start_ == 0){
stack.pop();
continue;
}
if (top->phase == INIT) {
int j = -1;
while (inorder[top->in_start_ + ++j] == preorder[top->pre_start_]) ;
top->left_tree_len = j;
int pre_start = top->pre_start_ + 1;
int pre_end = top->pre_start_ + j;
int in_start = top->in_start_;
int in_end = top->in_start_ + j
struct node* current = new node(preorder[pre_start], pre_start, pre_end, in_start, in_end);
top->left = current;
top->phase = LEFT;
stack.push(current);
} else if (top.phase == LEFT) {
int pre_start = top->pre_start_ + top->left_tree_len + 1;
int pre_end = top->pre_end_;
int in_start = top->in_start_ + top->left_tree_len + 1;
int in_end = top->in_end_;
struct node* current = new node(preorder[pre_start], pre_start,
pre_end, in_start, in_end);
top->right = current;
top->phase = RIGHT;
stack.push(current);
} else {
stack.pop();
}
}
return root;
}
void inorder_print(struct node* root) {
if (root == nullptr) return;
inorder_print(root->left);
std::cout << root->value << std::endl;
inorder_print(root->right);
}
int main() {
std::vector<int> pre_order = {4, 3, 7, 5, 6};
std::vector<int> in_order = {3, 4, 5, 7, 6};
struct node* root = buildBinaryTree(pre_order, 0, pre_order.size() - 1,
in_order, 0, in_order.size() - 1);
inorder_print(root);
}