1501 Binary Tree maximum width and height, 1501 Binary Tree width

Source: Internet
Author: User

1501 Binary Tree maximum width and height, 1501 Binary Tree width
1501 maximum Binary Tree width and height

 

Time Limit: 1 s space limit: 128000 KB title level: Silver Title Description Description

Returns the maximum width and height of a binary tree.

Input description Input Description

The first line is an integer n.

There are two numbers in each row in the next n rows. For the two numbers in row I, the numbers represent the numbers of the two left and right sons connected by the node numbered I. If no son is empty, it is 0.

Output description Output Description

The output is a row in total. The maximum width and height of the output binary tree are separated by a space.

Sample Input Sample Input

5

2 3

4 5

0 0

0 0

0 0

Sample output Sample Output

2 3

Data range and prompt Data Size & Hint

N <16

The first node is the root node by default.

Number in the input order

2-N + 1 indicates the left and right children of the node.

Note: The second question is extreme data!

1

0 0

You don't want to be opportunistic. You can search for this question honestly!

 

CATEGORY tag Tags click here to expand
 1 #include<iostream> 2 using namespace std; 3 int root; 4 int max_shendu=0; 5 int max_kuandu=1; 6 struct node 7 { 8     int parent; 9     int date;10     int lchild;11     int rchile;12     int sd;13 }a[101];14 int bl(int sd_now,int i)15 {16     //int flag1=0;17     //int flag2=0;18     a[i].sd=sd_now;19     if(a[i].sd>max_shendu)20     {21         max_shendu=a[i].sd;22     }23     if(a[i].lchild!=0)24     {25     //    flag1=1;26         bl(sd_now+1,a[i].lchild);27     }28     if(a[i].rchile!=0)29     {30     //    flag2=1;31         bl(sd_now+1,a[i].rchile);32     }33     /*if(flag1==1&&flag2==1)34     {35         max_kuandu++;36     }*/37 }38 int main()39 {40     int n;41     cin>>n;42     for(int i=1;i<=n;i++)43     {44         cin>>a[i].lchild>>a[i].rchile;45         a[a[i].lchild].parent=i;46         a[a[i].rchile].parent=i;47     }48     for(int i=1;i<=n;i++)49     {50         if(a[i].parent==0)51         {52             root=i;53             break;54         }55     }56     bl(0,root);57     for(int i=0;i<=max_shendu;i++)58     {59         int sum=0;60         for(int j=1;j<=n;j++)61         {62             if(a[j].sd==i)63             {64                 sum++;65             }66         }67         if(sum>max_kuandu)68         {69             max_kuandu=sum;70         }71     }72     cout<<max_kuandu<<" ";73     cout<<max_shendu+1;74     return 0;75 }

 

Related Article

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.