| Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
Trees are fundamental in many branches of computer science. Current state-of-the art parallel computers such as Thinking Machines‘ CM-5 are based on fat trees. Quad- and octal-trees are fundamental to many algorithms in computer graphics.
This problem involves building and traversing binary trees.
Given a sequence of binary trees, you are to write a program that prints a level-order traversal of each tree. In this problem each node of a binary tree contains a positive integer and all binary trees have have fewer than 256 nodes.
In a level-order traversal of a tree, the data in all nodes at a given level are printed in left-to-right order and all nodes at level k are printed before all nodes at level k+1.
For example, a level order traversal of the tree
is: 5, 4, 8, 11, 13, 4, 7, 2, 1.
In this problem a binary tree is specified by a sequence of pairs (n,s) where n is the value at the node whose path from the root is given by the string s. A path is given be a sequence of L‘s andR‘s where L indicates a left branch and R indicates a right branch. In the tree diagrammed above, the node containing 13 is specified by (13,RL), and the node containing 2 is specified by (2,LLR). The root node is specified by (5,) where the empty string indicates the path from the root to itself. A binary tree is considered to be completely specified if every node on all root-to-node paths in the tree is given a value exactly once.
The input is a sequence of binary trees specified as described above. Each tree in a sequence consists of several pairs (n,s) as described above separated by whitespace. The last entry in each tree is (). No whitespace appears between left and right parentheses.
All nodes contain a positive integer. Every tree in the input will consist of at least one node and no more than 256 nodes. Input is terminated by end-of-file.
For each completely specified binary tree in the input file, the level order traversal of that tree should be printed. If a tree is not completely specified, i.e., some node in the tree is NOT given a value or a node is given a value more than once, then the string ``not complete‘‘ should be printed.
(11,LL) (7,LLL) (8,R) (5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) () (3,L) (4,R) ()
5 4 8 11 13 4 7 2 1 not complete
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | #include <vector>#include <list>#include <map>#include <set>#include <deque>#include <queue>#include <stack>#include <bitset>#include <algorithm>#include <functional>#include <numeric>#include <utility>#include <sstream>#include <iostream>#include <iomanip>#include <cstdio>#include <cmath>#include <cstdlib>#include <cctype>#include <string>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <ctime>using namespace std;typedef long long LL;#define CLR(x,y) memset((x),(y),sizeof((x)))#define FOR(x,y,z) for(int (x)=(y);(x)<(z);(x)++)#define FORD(x,y,z) for(int (x)=(y);(x)>=(z);(x)--)#define FOR2(x,y,z) for((x)=(y);(x)<(z);(x)++)#define FORD2(x,y,z) for((x)=(y);(x)>=(z);(x)--)const int maxn = 256 + 100;char str[maxn],son[maxn];int value,error;struct node{ int v,have_value; node *lson,*rson; node():have_value(0),lson(NULL),rson(NULL){}};void addnode(node *head,int v,char s[]){ if(error) return ; char *p = s; if(*p == ‘)‘){ head->have_value = 1; head->v = v; return ; } while(*p){ if(*p == ‘L‘){ if(!head->lson) head->lson = new node; head = head->lson; } else if( *p == ‘R‘ ){ if(!head->rson) head->rson = new node; head = head->rson; }else { if(head->have_value){ error = 1; return ; } head->have_value = 1; head->v = v; } p++; }}int res[maxn];void bfs(node *head){ if(error){ puts("not complete"); return ; } queue<node*> q; q.push(head); int cnt = 0; while(!q.empty()){ node * cur = q.front();q.pop(); if(!cur->have_value){ puts("not complete"); return ; } res[cnt++] = cur->v; if(cur->lson) q.push(cur->lson); if(cur->rson) q.push(cur->rson); } FOR(i,0,cnt - 1) printf("%d ",res[i]); printf("%d\n",res[cnt - 1]);}void deltree(node * head){ if(head){ if(head->lson) deltree(head->lson); if(head->rson) deltree(head->rson); delete head; }}int main(){ node * root = new node; error = 0; while(~scanf("%s",str)){ if(!strcmp(str,"()")){ bfs(root); deltree(root); error = 0; root = new node; continue; } sscanf(str,"(%d,%s",&value,son); addnode(root,value,son); } return 0;} |
[2016-02-08][UVA][122][Trees on the level]
原文:http://www.cnblogs.com/qhy285571052/p/5185013.html