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

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

时间:2020-06-27 13:11:54      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:==   inf   node   ++   scan   idt   int   ref   code   

题目

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】二叉树深度

标签:==   inf   node   ++   scan   idt   int   ref   code   

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

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