Ultraviolet (a) problem 122-trees on the level

Source: Internet
Author: User
Tags dot net
// 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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.