-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1361. Validate Binary Tree Nodes.cpp
62 lines (50 loc) · 1.33 KB
/
1361. Validate Binary Tree Nodes.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Solution {
public:
int count(vector<int>& L,vector<int>& R,int node)
{
if(node==-1)
return 0;
int left=count(L,R,L[node]);
int right=count(L,R,R[node]);
return left+right+1;
}
bool validateBinaryTreeNodes(int n, vector<int>& L, vector<int>& R) {
//every node has indegree equal to 1 , except root (0)
//check for multiple roots - if yes then return 0 else return 1
//count node
//for indegree
vector<int> indegree(n,0);
for(int i=0;i<n;i++)
{
if(L[i]!=-1)
{
indegree[L[i]]++;
if(indegree[L[i]]>1)
return 0;
}
if(R[i]!=-1)
{
indegree[R[i]]++;
if(indegree[R[i]]>1)
return 0;
}
}
//for multiple roots
int root=-1;
for(int i=0;i<n;i++)
{
if(indegree[i]==0)
{
if(root==-1)
root=i;
else
return 0;
}
}
if(root==-1)
return 0;
//count nodes
int c=count(L,R,root);
return c==n?1:0;
}
};