文章

16

粉丝

33

获赞

2

访问

3.2k

头像
二叉树的后序序列 题解:
P4215
发布于2024年3月31日 23:36
阅读数 174

 看不懂下面的代码?我的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...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发