码迷,mamicode.com
首页 > 其他好文 > 详细

18.2.14 codevs1501 二叉树最大宽度和高度

时间:2018-02-14 17:33:55      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:strong   post   连接   输出   view   put   ...   head   整数   

题目描述 Description

    给出一个二叉树,输出它的最大宽度和高度。

输入描述 Input Description

第一行一个整数n。

下面n行每行有两个数,对于第i行的两个数,代表编号为i的节点所连接的两个左右儿子的编号。如果没有某个儿子为空,则为0。

输出描述 Output Description

输出共一行,输出二叉树的最大宽度和高度,用一个空格隔开。

样例输入 Sample Input

5

2 3

4 5

0 0

0 0

0 0

样例输出 Sample Output

2 3

数据范围及提示 Data Size & Hint

n<16

默认第一个是根节点

以输入的次序为编号

2-N+1行指的是这个节点的左孩子和右孩子

注意:第二题有极端数据!

          1

          0 0

这题你们别想投机取巧了,给我老老实实搜索!

技术分享图片
 1 #include <iostream>
 2 #include <math.h>
 3 #include<string.h>
 4 
 5 using namespace std;
 6 
 7 int not0[20][3],sum_l[20]={0},lmax=0;
 8 
 9 void tree(int w,int l)//w当前节点 l当前高度
10 {
11     sum_l[l]++;
12     if(not0[w][1]==0&&not0[w][2]==0)
13     {
14         if(lmax<l)
15             lmax=l;
16     }
17     else
18     {
19         if(not0[w][1]!=0)
20             tree(not0[w][1],l+1);
21         if(not0[w][2]!=0)
22             tree(not0[w][2],l+1);
23     }
24     return;
25 }
26 
27 int main()
28 {
29     int n;
30     cin>>n;
31     for(int i=1;i<=n;i++)
32         for(int j=1;j<=2;j++)
33         {
34             cin>>not0[i][j];
35         }
36     tree(1,1);
37     int max=0;
38     for(int i=1;i<=n;i++)
39     {
40         if(max<sum_l[i])
41             max=sum_l[i];
42     }
43     cout<<max<<" "<<lmax<<endl;
44     return 0;
45 }
View Code

查了一下二叉树的概念和二叉树宽度指的是什么(....

二叉树宽度:具有最多结点数的层中包含的结点数

18.2.14 codevs1501 二叉树最大宽度和高度

标签:strong   post   连接   输出   view   put   ...   head   整数   

原文地址:https://www.cnblogs.com/yalphait/p/8448540.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!