# Binary Tree hierarchy traversal and binary tree hierarchy Traversal

Source: Internet
Author: User

Binary Tree hierarchy traversal and binary tree hierarchy Traversal

Link to the question: Ultraviolet A 122 Tree on the level

Input a binary tree. Your task outputs the values of each node in the order from top to bottom and from left to right. Each node is given according to the moving sequence from the root node to it (L, indicating doing, R indicating right ). In the input, there is no space between the left and right brackets of each node, and a space is used to separate adjacent nodes. The input of each tree ends with an empty pair of brackets.

NOTE: If some nodes in the path from the root to a node are not provided in the input, or if the number of nodes exceeds 1, the not complete should be output, and the number of nodes cannot exceed 256.

Corresponding to the first group of input.

Sample input:

`(11,LL) (7,LLL) (8,R)(5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ()(3,L) (4,R) ()`
Sample output:

`5 4 8 11 13 4 7 2 1not complete`

Let's take a look at the code with detailed comments.

`# Include <cstdio> # include <iostream> # include <cstdlib> # include <cstring> # include <string> using namespace std; const int maxn = 1500 + 100; struct Node {bool is_value; // whether int v has been assigned; // Node value: Node * left, * right; Node (): is_value (0), left (NULL ), right (NULL) {}}; Node * root; Node * newnode () {return new Node () ;}// if the application fails, NULLbool failed is returned; // indicates whether the int ans [maxn], cnt; // storage Node array, and the counter void remove_tree (Node * u) {// release the memory if (u = = NULL) return; remove_tree (u-> left); remove_tree (u-> right); delete (u);} void addnode (int v, char * s) {// Add Node int nlen = strlen (s); Node * u = root; for (int I = 0; I <nlen; I ++) {// traverse from the root if (s [I] = 'l') {if (u-> left = NULL) u-> left = newnode (); u = u-> left;} else if (s [I] = 'R') {if (u-> right = NULL) u-> right = newnode (); u = u-> right ;}} if (u-> is_value) failed = true; // if the value has been assigned, mark failed u-> v = v; u-> is_value = True;} bool input_tree () {char s [maxn]; failed = false; root = newnode (); for (;) {if (scanf ("% s ", s )! = 1) return false; if (! Strcmp (s, "()") break; int v; sscanf (& s [1], "% d", & v ); // read from (the following number, read the number to v; addnode (v, strchr (s, ',') + 1 ); // This shows the convenience of character arrays. the pointer to a character can be directly seen as a string, from this position to '\ 0' // strchr returns the pointer to the character position found in the string, that is, return "," followed by the string} return true;} bool bfs () {// queues implement sequence traversal of Binary Trees Node * q [maxn], * u; // simulates queue q [0] = root Using arrays; int front = 0, rear = 1; while (front <rear) {u = q [front ++]; if (! U-> is_value) return false; ans [cnt ++] = u-> v; // Add the header node to ans or output if (u-> left! = NULL) q [rear ++] = u-> left; // Add the subnode to the queue if (u-> right! = NULL) q [rear ++] = u-> right;} return true;} int main () {while (1) {cnt = 0; if (! Input_tree () break; if (! Failed & bfs () {for (int I = 0; I <cnt; I ++) {if (I) printf (""); printf ("% d", ans [I]);} printf ("\ n");} else {printf ("not complete \ n ");} remove_tree (root);} return 0 ;}`

Related Keywords:
Related Article

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.