Hierarchical traversal of uva-122 trees

Source: Internet
Author: User

Test Instructions Summary: Input the information of each node of the binary tree, after the completion, in order to traverse the tree in a hierarchical sequence, and then the weight of each node to the output!


Note: If a node from the root to a leaf node is not given or given more than once in the input,

You should output "not complete". The number of nodes is not more than 256!


The code is as follows: (detailed comments in the code!) This code time is 9ms !

#include <iostream> #include <cstdio> #include <vector> #include <queue> #include <cstring >using namespace Std;const int Maxn=52014;char str[maxn];bool failed=false;vector<int>ans;struct node//node structure body!    {bool Have_value;    int value;    Node *left,*right; Node (): Have_value (False), value (0), left (null), right (null) {}};struct node *root;//build root! void Remove_tree (Node *now)//release the memory occupied by the tree!    {if (now==null) return;    Remove_tree (Now->left);    Remove_tree (Now->right); Delete now;} Node *newnode ()//Create a new node! {return new Node ();} void AddNode (int num,char *s)//Add node, assign value at the same time!    {int Len=strlen (s);    Node *now=root; for (int i=0; i<len; i++) {if (s[i]== ' L ') {if (now->left==null) now->le            Ft=newnode ();//Build Heart new node!        now=now->left;            } else if (s[i]== ' R ') {if (now->right==null) Now->right=newnode ();//Create a new node!        now=now->right; }   } if (Now->have_value) failed=true;//If the node is already assigned, then the input is wrong!    now->value=num;//Assigning a value to a node! now->have_value=true;//Flag The node has been assigned! }bool input ()//Enter the information for the Tree J node!    {failed=false;    Root=newnode ();        while (1) {if (scanf ("%s", str)!=1) return false;//error input if (!strcmp (str, "()")) break;//end input!        int num;//for the string "(11,,ll)", sscanf (&str[1], "%d", &num);//This operation is to remove the node weight value "11"! AddNode (NUM,STRCHR (str, ', ') +1); the//STRCHR (str, ', ') function is//returns a pointer to the first character ', ' from left to right in Str, so that the string corresponding to STRCHR (str, ', ') +1 is "LL"    ". } return true;    BOOL BFs () {queue<node*>q;//set up a queue!    Q.push (root);    Ans.clear ();//Initialize the container! while (!        Q.empty ()) {Node *now=q.front ();        Q.pop ();        if (!now->have_value) return false;//If the value of this node is not yet, then the input is wrong!        Ans.push_back (Now->value);//Store the value in the container!        if (now->left!=null) Q.push (now->left);    if (now->right!=null) Q.push (now->right); } return true;    void print () {for (int i=0; i<ans.size (); i++) printf ("%d%c", Ans[i],i==ans.size ()-1? '    \ n ': '); return;}            int main () {while (input ()) {if (failed) {cout<< ' not complete ' <<endl;        Continue        } if (BFS ()) print ();        else cout<< "not complete" <<endl;    Remove_tree (root); } return 0;}


Hierarchical traversal of uva-122 trees

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.