zoukankan      html  css  js  c++  java
  • 1501 二叉树最大宽度和高度

    题目描述 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

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

     

    听话,搜索。

    AC代码:

     1 #include<iostream>
     2 #include<cmath>
     3 #include<cstring>
     4 using namespace std;
     5 
     6 int n,Max=0,tall=0;
     7 int c[50],x[50],y[50];
     8 
     9 void dfs(int num,int h){
    10     if(num!=0){
    11         c[h]++;
    12         Max=max(Max,c[h]);
    13         tall=max(tall,h);
    14         dfs(x[num],h+1);
    15         dfs(y[num],h+1);
    16     }
    17 }
    18 
    19 int main(){
    20     cin>>n;
    21     memset(c,0,sizeof(c));
    22     memset(x,0,sizeof(x));
    23     memset(y,0,sizeof(y));
    24     for(int i=1;i<=n;i++){
    25         cin>>x[i]>>y[i];
    26     }
    27     dfs(1,1);
    28     cout<<Max<<" "<<tall<<endl;
    29     return 0;
    30 }
  • 相关阅读:
    BZOJ1087=Codevs2451=洛谷P1896&P2326互不侵犯
    poj1286
    P1066 2^k进制数
    开车旅行
    洛谷P1396 营救
    poj1840
    poj3693
    poj1195
    3955 最长严格上升子序列(加强版)
    1021 玛丽卡
  • 原文地址:https://www.cnblogs.com/Kiven5197/p/5665571.html
Copyright © 2011-2022 走看看