#include <stdio.h>
#include <iostream>
#include <climits>
using namespace std;
const int maxn = 1000010;
int ans = INT_MIN;
struct node {
int l;
int r;
};
node Tree[maxn];
//到叶子结点了
void dfs(int root, int depth)
{
if (Tree[root].l == 0 && Tree[root].r == 0)
{
ans = depth > ans ? depth : ans;
}
else {
if (Tree[root].l != 0)
{
dfs(Tree[root].l, depth+1);
}
if (Tree[root].r != 0)
{
dfs(Tree[root].r, depth + 1);
}
}
}
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> Tree[i].l >> Tree[i].r;
}
dfs(1, 1);
cout << ans;
}
P4913 【深基16.例3】二叉树深度 题解 C++
最新推荐文章于 2024-06-24 22:40:49 发布