UVA 699 Falling Leaves (p159, DFS of the binary tree)

Source: Internet
Author: User
Tags printf

Analysis: The input of this problem is the first order traversal, it is important to find this. Count the total number of leaves at each level, and you can think of the whole tree as a one-dimensional coordinate system.



#include <stdio.h>
int a[81],left,right;//Chronicle left and right boundary
void dfs (int num,int pos)
{
    int x, y;
    if (num!=-1)
    {
        if (pos<left) Left=pos;
        if (pos>right) Right=pos;
        A[pos]+=num;
        scanf ("%d", &x);//Left Dial hand tree
        DFS (x,pos-1);
        scanf ("%d", &y);//Right Sub-tree
        dfs (y,pos+1);
    }
};
int  Main ()
{
    int i,n,sum=0;
    while (scanf ("%d", &n) &&n!=-1)
    {for
        (i=0;i<81;i++) a[i]=0;
        ++sum;
        left=40;right=40;
        DFS (n,40);
        printf ("Case%d:\n", sum);
        for (i=left;i<right;i++)
        printf ("%d", A[i]);
        printf ("%d\n\n", A[right]);
    }
    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.