UVa 699:the falling Leaves Two-forked tree leaves

Source: Internet
Author: User
Tags cas printf stdin

Topic Link:

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_ problem&problem=640

Type of topic: data structure, two fork tree

The main effect of the topic:

Every autumn, the leaves of the North are accompanied by brilliant colors, leaves falling with the wind under the tree, the ground soon accumulated a large pile.

If the same thing happens in a binary tree, and the nodes in the tree slowly fall down, what is the scene?

This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45530.htm

Two-fork trees are also trees.

For a binary tree, each node is treated as a branch, and the value on the node is the number of leaves on that branch. For each node, it is a unit of distance from the left son and the right son.

As shown above, the root nodes 5 and 6 are on the same vertical line, and 7 and 5 are separated by one unit.

Then, autumn came, the leaves of the binary tree fell, and the weather is very good, there is no wind, so the two fork tree leaves are vertical fall down. Finally, the leaves of the same vertical line are all falling on the same pile.

On Let's find out the number of leaves on each pile.

Ideas for solving problems:

The topic gives a string of numbers, which is given in the order of the preceding sequence,-1 indicates that the node is empty.

First you can construct the binary tree recursively, and then search for a pile of numbers.

The trick I used was to open a 1000 size array result, 500 coordinate position stub node, then recursion, go to the left son, coordinates-1, go to the right son, coordinates +1.

This problem can also be used without achievement, can be calculated directly on the recursive results. Here are two versions of the code.

Sample input:

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

Sample output:

Case 1:
7-3 Case

2:
9 7 21 15

Version One: Build edition

#include <iostream> #include <cstdio> #include <cstring> using namespace std;  
const int end =-2147483645;  
     
int arr[1000], asize, result[1000];  
    Class node{Public:int data;  
    Node *father;  
    Node *left;  
Node *right;  
};  
Node node[1000];  
     
int Indexnode, POS;  
    inline node* buildroot (int n) {indexnode = 1;  
    Node[0].data = n;  
    Node[0].father = NULL;  
    Node[0].left = Node[0].right = NULL;  
Return &node[0];  
    } inline node* NewNode () {node[indexnode].left = NULL;  
    Node[indexnode].right = NULL;  
Return &node[indexNode++];  
    } node* Build (Node *root) {if (pos>=asize) return NULL;  
    if (arr[pos]==-1) {return NULL;  
    root = NewNode ();  
    Root->data = Arr[pos];  
    ++pos;  
    Root->left = Build (Root->left);     
    ++pos;  
    Root->right = Build (Root->right);  
return root; } void PreordeR (Node *root) {if (root) {printf ("%d", root->data);  
        Preorder (Root->left);  
    Preorder (root->right);  
    } void Dfs (Node *root, int index) {if (root) {Result[index] + = root->data;  
        printf ("%d:%d,", Root->data, index);  
        DFS (Root->left, index-1);  
    DFS (Root->right, index+1);  
    } void Solve () {Node *root=null;  
    pos=0;  
    Indexnode = 0;  
    Root = Build (root);  
    memset (result, 0, sizeof (result));  
    DFS (root, 500);  
    int i=0;  
    while (result[i]==0) ++i;  
    printf ("%d", result[i++]);   
    for (; result[i]!=0; ++i) printf ("%d", result[i]);     
printf ("\ n \ nthe");  
    int main () {freopen ("Input.txt", "R", stdin);  
    int Cas=1;  
        while (~SCANF ("%d", &arr[0]) && arr[0]!=-1) {asize = 1;  
        int cnt1=1, cnt2=0; while (GetChar ()) {scanf ("%d"), &arr[asize++]);  
            if (arr[asize-1]==-1) ++cnt2;  
            else ++cnt1;  
        if (cnt1+1==cnt2) break;  
        printf ("Case%d:\n", cas++);  
    Solve ();  
return 0; }

Version Two: no build version

#include <iostream> #include <cstdio> #include <cstring> using namespace std;  
     
int arr[1000], asize, result[1000];  
     
int Indexnode, POS;  
    void build (int index) {if (pos>=asize) return;  
    if (arr[pos]==-1) {return;  
    } Result[index] + = Arr[pos];  

    ++pos;  
    Build (Index-1);     
    ++pos;  
Build (index+1);  
    } void Solve () {pos=0;  
    Indexnode = 0;  
    memset (result, 0, sizeof (result));  
    Build (500);  
    int i=0;  
    while (result[i]==0) ++i;  
    printf ("%d", result[i++]);   
    for (; result[i]!=0; ++i) printf ("%d", result[i]);     
printf ("\ n \ nthe");  
    int main () {//Freopen ("Input.txt", "R", stdin);  
    int Cas=1;  
        while (~SCANF ("%d", &arr[0]) && arr[0]!=-1) {asize = 1;  
        int cnt1=1, cnt2=0;  
            while (GetChar ()) {scanf ("%d", &arr[asize++]);  
         if (arr[asize-1]==-1)       ++cnt2;  
            else ++cnt1;  
        if (cnt1+1==cnt2) break;  
        printf ("Case%d:\n", cas++);  
    Solve ();  
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.