122 - Trees on the level
来源:互联网 发布:软件检测机构行业 编辑:程序博客网 时间:2024/06/05 00:49
#include<iostream>#include<cstdio>#include<string.h>#include<queue>using namespace std;const int maxn=10000+5;struct Node{ bool have_value; int v; Node *left,*right; Node():have_value(false),left(NULL),right(NULL){}};bool failed;Node* tree;char s[maxn];Node* new_node(){ return new Node();}bool addnode(int v,char* s){ int n=strlen(s); Node* node=tree; for(int i=0;i<n;i++){ if('L'==s[i]){ if(node->left==NULL)node->left=new_node(); node=node->left; } if('R'==s[i]){ if(node->right==NULL)node->right=new_node(); node=node->right; }} if(node->have_value)return false;//重复输入 node->v=v; node->have_value=true; return true;}void remove_tree(Node* tree){ if(tree==NULL) return; remove_tree(tree->left); remove_tree(tree->right); delete tree;}void bfs(){ queue<Node*> nque; nque.push(tree); while(!nque.empty()){ Node* node=nque.front(); nque.pop(); if(!node->have_value){printf("not complete\n");return;} printf("%d ",node->v); if(node->left!=NULL)nque.push(node->left); if(node->right!=NULL)nque.push(node->right); } printf("\n");}void read_input(){ tree=new_node(); failed=false; while(scanf("%s",s)==1){ if(!strcmp("()",s)){bfs();remove_tree(tree);tree=new_node();} else{ int v; sscanf(&s[1],"%d",&v); if(!addnode(v,strchr(s,',')+1))failed=true; } }}int main(){ read_input(); return 0;}
0 0
- 122 - Trees on the level
- 122 - Trees on the level
- 122 - Trees on the level
- 122 Trees on the level
- TREES ON THE LEVEL
- Trees on the level
- Trees on the level
- Trees on the level
- UVa Problem 122 - Trees on the level
- UVaOJ 122 - Trees on the level
- UVA 122 - Trees on the level
- uva 122 - Trees on the level
- uva 122 Trees on the level
- UVA-122-Trees on the level
- UVA - 122 Trees on the level
- UVa 122 - Trees on the level
- UVa 122 - Trees on the level
- Uva 122 Trees on the level
- jmp short
- 【HDU 5984 Pocky】+ 数学
- Java之Instance关键字
- POJ 3009 Curling 2.0 已被翻译
- 【B/S初步】——B/S vs C/S
- 122 - Trees on the level
- LeetCode 38. Count and Say
- J2SE总结
- 中国哲学简史
- 设计交织笔记
- 利用iframe实现伪ajax上传文件
- WM_CTLCOLOR消息
- 小程序https Android (安卓) 可以发请求,IOS (苹果)发请求失败问题
- 7. Reverse Integer