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

题目

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);
}
原文地址:https://www.cnblogs.com/Jason66661010/p/13197964.html