首页 > 其他 > 详细

二叉树 P4913 【深基16.例3】二叉树深度

时间:2020-06-27 13:15:40      阅读:108      评论:0      收藏:0      [点我收藏+]

题目

https://www.luogu.com.cn/problem/P4913

技术分享图片

 

 代码

#include<iostream>
#include<cstdio>
struct node
{
    int left;
    int right;
}list[1000001];
int depth = 0;
void run(int root,int d)
{
    if (list[root].left == list[root].right&&list[root].left == 0)
    {
        if (d > depth)depth = d;
        return;
    }
    if(list[root].left!=0)
    run(list[root].left, d + 1);
    if(list[root].right!=0)
    run(list[root].right, d + 1);


}
int n;
int main()
{
    scanf("%d", &n);
    for (int i =1; i <= n; i++)
        scanf("%d%d", &list[i].left, &list[i].right);
    run(1, 1);
    printf("%d", depth);
}

 

二叉树 P4913 【深基16.例3】二叉树深度

原文:https://www.cnblogs.com/Jason66661010/p/13197964.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!