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

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

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

     

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int m,n,sd,sum=0;
    int abs[10000];
    struct node
    {
        int z;
        int y;
    }tree[1000];
    void dfs(int x,int step)
    {
        if(step>sum)
        sum=step;
        if(tree[x].z!=0)
        {
            abs[step]++;
            dfs(tree[x].z,step+1);
        }
        if(tree[x].y!=0)
        {
            abs[step]++;
            dfs(tree[x].y,step+1);
        }
        else return;
    }
    int main()
    {
        int n,jk=-1;;
        cin>>n;
        if(n==1)
        jk=1;
        for(int i=1;i<=n;i++)
        {
            cin>>tree[i].z>>tree[i].y;
            
        }
        dfs(1,1);
        
        for(int i=1;i<=sum;i++)
        {
            if(jk<abs[i])swap(jk,abs[i]);
        }
        cout<<jk<<" "<<sum;
        return 0;
    }
  • 相关阅读:
    python中自定义模块导入
    EditText------Android
    Fragment类实现
    Android文件访问
    python中pip使用国内镜像提高安装速度
    esri/geometry包 (arcgis api for js)
    【CSDN 编辑器 MarkDowm 使用技巧】
    for 循环 :从指定下标开始,并指定步长
    【车牌识别】-车牌中字符分割代码详解
    【 Linux 常用命令】
  • 原文地址:https://www.cnblogs.com/sssy/p/6648733.html
Copyright © 2011-2022 走看看