文章
43
粉丝
24
获赞
292
访问
6.5k
#include<bits/stdc++.h>
using namespace std;
typedef struct node {
char data;
struct node* left, * right;
}* bt;
string qian,zhong;//前序和中序遍历序列
int cnt=0;//用于记录当前根节点在先序遍历位置
void createTree(int low, int high, bt &T) {//注意&,否则无结果
if (low > high){
T=NULL;
}
//在中序遍历中查找对应根结点
for (int i = low; i <= high; i++) { //只是为了找到根节点,没有更深的逻辑
if (zhong[i] == qian[cnt]) {//找到了根节点,对根节点处理
cnt++;//子树的根节点在先序遍历中的下标
//创建结点
T = new node;
T->data = zhong[i];
createTree(low, i - 1, T-...
登录后发布评论
暂无评论,来抢沙发