UVA 399 The Falling Leaves (binary tree)

Source: Internet
Author: User

The Falling Leaves

Each year, fall in the North central region is accompanied by the brilliant colors of the leaves on the trees, followed Qu Ickly by the falling leaves accumulating under the trees. If the same thing happened to binary trees, how large would the piles of leaves become?


We assume each node in a binary tree "drops" a number of leaves equal to the integer value stored in that node. We also assume that these leaves drop vertically to the ground (thankfully, there ' s no wind to blow them around). Finally, we assume that the nodes is positioned horizontally in such a manner so the left and right children of a node is exactly one unit to the left and one unit to the right, respectively, of their parent. Consider the following tree:

The nodes containing 5 and 6 has the same horizontal position (with different vertical positions, of course). The node containing 7 is one unit to the left of those containing 5 and 6, and the node containing 3 are one unit to their Right. When the ' leaves ' drop from these nodes, three piles is created:the leftmost one contains 7 leaves (from the leftmost no DE), the next contains (from the nodes containing 5 and 6), and the rightmost pile contains 3. (While it's true that the leaf nodes in a tree would logically has leaves, we ignore that in this problem.)

InputThe input contains multiple test cases, each describing a single tree. A tree is specified by giving the value in the root node, followed by the description of the left subtree, and then the de Scription of the right subtree. If a subtree is empty, the value-1is supplied. Thus The tree shown above is specified as5 7-1 6-1-1 3-1-1. Each actual tree node contains a positive, Non-zero value. The last test case was followed by a single-1(which would otherwise represent an empty tree).

Outputfor each test case, display the case number (they is numbered sequentially, starting with 1) on a line by itself. On the next line display the number of "leaves" in each pile, from left to right, with a single space separating each valu E. This display must start in column 1, and would not exceed the width of a 80-character line. Follow the output for each case by a blank line. This format was illustrated in the examples below.

Sample Input

5 7-1 6-1-1 3-1-18 2 9-1-1 6 5-1-1 12-1-1 3 7-1 -1-1-1

Sample Output
Case 1:7 3Case 2:9 7 21 15

Binary tree achievements can be.

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Vector>typedef Long long ll;using namespace std; #define REPF (I, A, b) for (int i = A; I <= B; + + i) #define R EP (i, n) for (int i = 0; i < n; + + i) #define CLEAR (A, X) memset (A, x, sizeof a) typedef struct node{I    NT data;    struct Node *lchild; struct Node *rchild;} Node; Node *t;int Num[550];int x,n;    Node *creattree (int n) {if (n==-1) return NULL;    Node *t= (node*) malloc (sizeof (node));    t->data=n;    t->lchild=t->rchild=null;    cin>>x;    T->lchild=creattree (x);    cin>>x;    T->rchild=creattree (x); return t;}    void Dfs (Node *t,int cnt) {if (t==null) return;    num[cnt]+=t->data;    DFS (T-&GT;LCHILD,CNT-1); DFS (t->rchild,cnt+1);}    int main () {int cas=0;        while (cin>>n&&n!=-1) {CLEAR (num,0);        T=creattree (n);        DFS (t,50);        int flag=0; cout<< "Case "<<++cas<<": "<<endl;                for (int i=0;i<100;i++) {if (Num[i]) {if (!flag) cout<<num[i];                else cout<< "" <<num[i];            flag=1;    }} cout<<endl<<endl; } return 0;}


UVA 399 The Falling Leaves (binary tree)

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.