文章
16
粉丝
66
获赞
4
访问
9.1k
看不懂下面的代码?我的CSDN博客有详细的代码过程分析,欢迎查看:【博客传送门】
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstring>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
struct treenode {
char data;
treenode *left;
treenode *right;
};
void aftervisit(treenode *root) {
if (root == NULL) {
return;
}
aftervisit(root->left);
aftervisit(root->right);
printf("%c", root->data);
}
treenode *rebuild(string pre, string mid) {
if (pre.size() == 0) {
return NULL;
} else {
char root_data = pre[0];
int root_index = mid.find(root_data);
//分割子树
string pre_l = pre.substr(1, root_index);//左子树的pre序列
string pre_r = pre.substr(root_index + 1);//右子树的pre序列
string mid_l = mid.substr(0, root_index);//左子树的mid序列
string mid_r = mid.substr(root_index + 1);//右子树的mid序列
treenode *newnode = new treenode;
newnode->data = root_data;
newnode->left = reb...
登录后发布评论
暂无评论,来抢沙发