-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDS-1.2.TreeHeight.cpp
More file actions
47 lines (41 loc) · 966 Bytes
/
DS-1.2.TreeHeight.cpp
File metadata and controls
47 lines (41 loc) · 966 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<bits/stdc++.h>
using namespace std;
long long heightTree( vector <vector <long long> > children, long long n, long long root)
{
long long height=0;
queue <int> q;
q.push(root);
while(true)
{
int level=q.size();
if(q.size()==0)
return height;
else{
height++;
while(level--)
{
int x =q.front();
q.pop();
for(int i=0;i<children[x].size();i++)
q.push(children[x][i]);
}
}
}
return height;
}
int main() {
long long n,x,root;
cin>>n;
vector <vector <long long> > children;
children.resize(n);
for(long long i=0;i<n;i++)
{
cin>>x;
if(x==-1)
{root = i;}
else
{
children[x].push_back(i);}
}
cout<<heightTree(children,n, root);
}