// Ultraviolet (a) problem 122-trees on the level // verdict: accepted // submission date: 2011-12-25 // ultraviolet (a) Run Time: 0.008 S // All Rights Reserved (c) 2011, Qiu. Metaphysis # Yeah dot net // [solution] // This question can be attributed to a data structure problem. The step is to create a tree structure and check whether it is complete. If it is complete, the nodes are output in depth. # Include <iostream> # include <sstream> # include <cstring> using namespace STD; # define tag 0 # define maxn 256 struct node {struct node * parent; struct node * childleft, * childright; int value ;}; bool nocomplete, printwhitespace; int cache [maxn] [maxn]; int count [maxn]; void checkcomplete (node * Current) {If (current-> value = tag) nocomplete = true; If (current-> childleft! = NULL) checkcomplete (current-> childleft); If (current-> childright! = NULL) checkcomplete (current-> childright);} // traverses the tree and checks whether the path saved by the leaf node is the target value. Void traveltree (node * Current, int depth) {cache [depth] [count [depth] ++] = Current-> value; If (current-> childleft! = NULL) traveltree (current-> childleft, depth + 1); If (current-> childright! = NULL) traveltree (current-> childright, depth + 1);} void cleartree (node * Current) {If (current-> childleft! = NULL) cleartree (current-> childleft); If (current-> childright! = NULL) cleartree (current-> childright); Delete current;} int main (INT argc, char const * argv []) {char C; node * root = new node; root-> childleft = NULL; root-> childright = NULL; root-> value = tag; while (CIN> C) {If (C = '') continue; if (C = '(') {string pairs; while (CIN> C, C! = ') Pairs + = C; If (pairs. length () {istringstream ISS (pairs); int number; string positions; ISS> number> C> positions; node * Current = root; for (INT I = 0; I <positions. length (); I ++) {If (positions [I] = 'l') {If (current-> childleft = NULL) {node * temp = new node; temp-> value = tag; temp-> childleft = NULL; temp-> childright = NULL; Current-> childleft = temp;} current = Current-> childleft ;} else {If (current-> childright = NULL) {node * temp = new node; temp-> value = tag; temp-> childleft = NULL; temp-> childright = NULL; Current-> childright = temp;} current = Current-> childright;} If (current-> value> 0) Current-> value = tag; elsecurrent-> value = number;} else {nocomplete = false; checkcomplete (Root); If (nocomplete) cout <"not complete \ n"; else {memset (count, 0, sizeof (count); traveltree (root, 0); printwhitespace = false; For (INT I = 0; I <maxn; I ++) for (Int J = 0; j <count [I]; j ++) {If (printwhitespace) cout <"; elseprintwhitespace = true; cout <cache [I] [J];} cout <Endl;} cleartree (Root); root = new node; root-> childleft = NULL; root-> childright = NULL; root-> value = tag ;}} return 0 ;}