6 solutions
-
9
最短代码,详解(看注释),需要支持,求赞~
#include <bits/stdc++.h> using namespace std; struct trees { char lc, rc; // left_child:左孩子;right_child:右孩子 } tree[130]; // ASCLL码最大127, 130刚刚好 char h, root; // root表示根节点 int n; // 二叉树的节点数 n void find(char x) { if (x == '*') return; // 如果是'*'就直接return cout << x; // 输出 find(tree[x].lc); // 左边遍历 find(tree[x].rc); // 右边遍历 } int main() { cin >> n >> root >> tree[root].lc >> tree[root].rc; for (int i = 2; i <= n; i++) cin >> h >> tree[h].lc >> tree[h].rc; find(root); // 开始前序遍历 return 0; }
-
0
#include<bits/stdc++.h> using namespace std; struct node{ //char a; int l,r; }lll[344]; void bfs(int b){ printf("%c",b+'a'); if(lll[b].l!=-114514){ bfs(lll[b].l); } if(lll[b].r!=-114514){ bfs(lll[b].r); } } int main(){ int n,root; cin>>n; for(int i=0;i<=n;i++){ char c,lc,rc; cin>>c>>lc>>rc; if(i==0){ root=c-'a'; } if(lc!='*'){//这个节点有孩子 lll[c-'a'].l=lc-'a'; } if(lc=='*'){//这个节点mei孩子 lll[c-'a'].l=-114514; } if(rc!='*'){//这个节点有孩子 lll[c-'a'].r=rc-'a'; } if(rc=='*'){//这个节点mei孩子 lll[c-'a'].r=-114514; } } bfs(root); }****
-
0
#include<iostream> using namespace std; struct node{ char dat; int lyc,ryc; }tree[111]; void f(int h){ cout<<tree[h].dat; if(tree[h].lyc!=-1) { f(tree[h].lyc); } if(tree[h].ryc!=-1) { f(tree[h].ryc); } } int main(){ int n,root; cin>>n; char x,lc,rc; for(int i=1;i<=n;i++){ cin>>x>>lc>>rc; if(i==1){ root=x-'a'; } node nd({x, lc-'a', rc-'a'}); tree[x-'a']=nd; if(lc=='*'){ tree[x-'a'].lyc=-1; } if(rc=='*'){ tree[x-'a'].ryc=-1; } } f(root); }
-
-1
#include <bits/stdc++.h> using namespace std; struct node{ char dat; int lc,rc; }tree[110]; void g(int n){ cout<<tree[n].dat; if(tree[n].lc!=-1) g(tree[n].lc); if(tree[n].rc!=-1) g(tree[n].rc); } int main(){ int n,root; char x,lc,rc; cin>>n; for(int i=1;i<=n;i++){ cin>>x>>lc>>rc; if(i==1) root=x-'a'; node nd({x,lc-'a',rc-'a'}); tree[x-'a']=nd; if(lc=='*') tree[x-'a'].lc=-1; if(rc=='*') tree[x-'a'].rc=-1; } g(root); return 0; }
-
-2
#include<bits/stdc++.h> using namespace std; struct node{ int dat; int lc,rc; }tree[200]; void gg(int y){ //cout<<"XXX"<<tree[y].rc<<"\n"; cout<<char(tree[y].dat); if(tree[y].lc!=-1) gg(tree[y].lc); if(tree[y].rc!=-1) gg(tree[y].rc); } int main(){ int root,n; cin>>n; char rc1,lc1,x; for(int i=1;i<=n;i++){ cin>>x>>lc1>>rc1; if(i==1) root=x; node nd({x, lc1, rc1}); tree[x]=nd; if(lc1=='*') tree[x].lc=-1; if(rc1=='*') tree[x].rc=-1; } gg(root); return 0; }
-
-8
#include<iostream> using namespace std; struct node{//节点 int a,l,r;//储存数据、左子节点、右子节点 }a[26];//储存排序规则:a(大的)[n].a(小的)=n void dg(node x); int main(){ int n; cin>>n; int root;//树根 for(int i=0;i<n;i++){ string in; cin>>in; if(!i) root=in[0]-'a';//标记树根 node x={in[0]-'a',in[1]-'a',in[2]-'a'};//push进去的节点 if(in[1]-'*'); else x.l=-1;//标记* if(in[2]-'*'); else x.r=-1;//标记* a[x.a]=x;//储存,遵循规则 a[n].a=n } dg(a[root]);//递归 } void dg(node x){ cout<<char(x.a+'a');//前序排列,先输你爹 if(x.l+1)//左边有人 dg(a[x.l]);//递归左边 if(x.r+1)//31、32行同理 dg(a[x.r]); }//如果题目要中序或后序,改变递归函数中的顺序就可以了
- 1
Information
- ID
- 963
- Time
- 1000ms
- Memory
- 125MiB
- Difficulty
- 5
- Tags
- # Submissions
- 97
- Accepted
- 41
- Uploaded By