文章
17
粉丝
0
获赞
92
访问
2.2k
学的是代码随想录的方法,代码中包括如何打印切割后的左右子树
#include <bits/stdc++.h>
using namespace std;
struct TreeNode{
char val;
struct TreeNode *left,*right;
TreeNode(char c):val(c),left(NULL),right(NULL){}
};
TreeNode* build(const string& pre,const string& in){
if(pre.size()==0) return NULL;
char rootValue = pre[0];
TreeNode* root = new TreeNode(rootValue);
// 寻找切割点
int index = 0;
for(index=0;index<in.size();++index){
if(in[index]==rootValue){
break;
}
}
//切割中序数组
string leftin(in.begin(),in.begin()+index);
// cout<<"leftin"<<leftin<<"\n";
...
登录后发布评论
暂无评论,来抢沙发