文章
52
粉丝
68
获赞
22
访问
11.5k
#include <bits/stdc++.h>
using namespace std;
struct tnode{
char val;
tnode *left,*right;
tnode():val(' '),left(nullptr),right(nullptr){}
tnode(char c):val(c),left(nullptr),right(nullptr){}
};
//寻找式插入
tnode* insert(tnode*root,char val){
if(root==nullptr)return new tnode(val);
if(val<root->val)root->left=insert(root->left,val);
else if(val>root->val)root->right=insert(root->right,val);
return root;
}
tnode *buildTree(string s){
tnode *root=nullptr;
for(auto x:s){
root=insert(root,x);
}
return root;
}
void preOrder(tnode*root,string &ans){
if(root==nullptr)return;
ans.push_back(root->val);
preOrder(root->left,ans);
preOrder(root->right,ans);
}
void inOrder(tnode*root,string &ans){
if(root==nullptr)return;
inOrder(root->left,ans);
ans.push_back(root->val);
inOrder(root->right,ans);
}
int main(){
...
登录后发布评论
暂无评论,来抢沙发