紫书章六例题七 树的层次遍历 UVA 122(链表)
来源:互联网 发布:狼蛛斩月灯光编程 编辑:程序博客网 时间:2024/06/05 16:47
开始看错了题目以为是一颗满二叉树,然后就很开心地开了一个数组。。最后RE。因为 题目中说的节点数是256,不一定是满的,如果全是左孩子,这个时候数组就大的不得了啦。然后可以用链表的方法
用数组模拟链表
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <sstream>#include <queue>using namespace std;struct node{ int v; int l,r;};node tree[300];int sum=1;struct node1{ int v,xb;};void judge(int i){ if(tree[i].l) {sum++;judge(tree[i].l);} if(tree[i].r) {sum++;judge(tree[i].r);}}void level(){ queue <node1> q; q.push((node1){tree[1].v,1}); int gg=0; while(!q.empty()) { node1 t=q.front(); q.pop(); if(gg) printf(" "); printf("%d",t.v); gg=1; if(tree[t.xb].l) q.push((node1){tree[tree[t.xb].l].v,tree[t.xb].l}); if(tree[t.xb].r) q.push((node1){tree[tree[t.xb].r].v,tree[t.xb].r}); }}int main(){ //freopen("E:\\input.txt","r",stdin); string str; memset(tree,0,sizeof(tree)); int flag=0,now=1,num=0; while(cin>>str) { if(str=="()") { if(flag||!tree[1].v) printf("not complete\n"); else{ judge(1); if( sum == num ) { level();printf("\n");} else printf("not complete\n"); } num=0;sum=1; memset(tree,0,sizeof(tree)); now=1,flag=0; } else{ num++; int cur=1; int k=str.find(','); string a1=str.substr(1,k-1); int len=str.length()-k-2; string a2=str.substr(k+1,len); stringstream ss(a1); int v; ss>>v; int temp=1; if(len==0) {if(!tree[1].v) tree[1].v=v;else flag=1;} else{ for(int i=0;i<len;i++) { if(a2[i]=='L') { if(tree[temp].l) temp=tree[temp].l; else { tree[temp].l=++now; temp=tree[temp].l; } } else if(a2[i]=='R') { if(tree[temp].r) temp=tree[temp].r; else { tree[temp].r=++now; temp=tree[temp].r; } } } if(tree[temp].v) flag=1; else tree[temp].v=v; } } } return 0;}
然后用指针来写链表
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <sstream>#include <queue>using namespace std;struct node{ int v; node *l,*r; node(int a=0,node *left=NULL,node *right=NULL) { v=a,l=left,r=right; }};node *root;int sum=1;void judge(node *t){ if(t->l) {sum++;judge(t->l);} if(t->r) {sum++;judge(t->r);}}void level(){ queue <node*> q;//注意这里的类型 node* q.push(root); int gg=0; while(!q.empty()) { node *t=q.front(); q.pop(); if(gg) printf(" "); printf("%d",t->v); gg=1; if(t->l!=NULL) q.push(t->l); if(t->r!=NULL) q.push(t->r); }}void remove_tree(node *u){ if(u == NULL ) return ; remove_tree(u->l); remove_tree(u->r); delete u;//new 用 delete,malloc 用free}int main()//注意要清空{ //freopen("E:\\input.txt","r",stdin); string str; int flag=0,now=1,num=0; root=new node(); while(cin>>str) { if(str=="()") { if(flag||!root->v) printf("not complete\n"); else{ judge(root); if( sum == num ) { level();printf("\n");} else printf("not complete\n"); } remove_tree(root); root=new node(); num=0;sum=1,flag=0; } else{ num++; int cur=1; int k=str.find(','); string a1=str.substr(1,k-1); int len=str.length()-k-2; string a2=str.substr(k+1,len); stringstream ss(a1); int v; ss>>v; node *temp=root; if(len==0) {if(!root->v) root->v=v;else flag=1;} else{ for(int i=0;i<len;i++) { if(a2[i]=='L') { if(temp->l!=NULL) temp=temp->l; else { temp->l=new node(); temp=temp->l; } } else if(a2[i]=='R') { if(temp->r!=NULL) temp=temp->r; else { temp->r=new node(); temp=temp->r; } } } if(temp->v) flag=1; else temp->v=v; } } } return 0;}
0 0
- 紫书章六例题七 树的层次遍历 UVA 122(链表)
- 例题6-7 树的层次遍历(Trees on the level, Duke 1993, UVa 122)
- 例题 6-7 树的层次遍历 UVa 122 建树+宽度优先遍历
- 例题6-7 UVA 122 - Trees on the level 树的层次遍历
- 例题6-7 二叉树的层次遍历 UVa 122 Trees on the level
- uva-122 树的层次遍历
- UVA 122 树的层次遍历
- UVA 122 树的层次遍历
- uva 122 树的层次遍历
- UVA 122 树的层次遍历
- uva-122 树的层次遍历
- 例题6-7 树的层次遍历 BFS遍历
- 例题6-7 树的层次遍历 UVa122
- 树得层次遍历@例题6-7
- uva 122 Trees on the level 二叉树的层次遍历(bfs)
- 紫书章六例题10 下落的树叶 UVA 699 (根据前序遍历建树)
- Uva 11234 Expressions (二叉树的层次遍历)
- UVA 122 Trees on the level (二叉树层次遍历)
- 矩阵
- Android动画系列(三)
- Android 6.0 ActivityCompat权限声明
- Java WebService 简单实例
- Linux编程之gdb调试
- 紫书章六例题七 树的层次遍历 UVA 122(链表)
- 魔术师的猜牌术
- 浅谈Ajax的适用场景和不适用场景
- 欢迎使用CSDN-markdown编辑器
- 我的梦想 从这里起飞
- 2017上半年课程设计递归斐波那契
- GDB 常用调试命令
- 字符流FileReader和FileWriter
- Spring事务管理(未完待续)