-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1361_Validate_Binary_Tree_Nodes.cpp
More file actions
46 lines (33 loc) · 1.08 KB
/
1361_Validate_Binary_Tree_Nodes.cpp
File metadata and controls
46 lines (33 loc) · 1.08 KB
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
class Solution {
public:
public:
bool global = true;
void dfs(int src,vector<int>& leftChild,vector<int>& rightChild,vector<bool>& visited)
{
if(visited[src]) {
global = false;
return ;
};
visited[src] = true;
if(leftChild[src]!=-1)
dfs(leftChild[src],leftChild,rightChild,visited);
if(rightChild[src]!=-1 )
dfs(rightChild[src],leftChild,rightChild,visited);
}
bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {
if(n<=1)
return true;
vector<bool> visited(n,false);
for(int i=0;i<n;i++)
{
if(leftChild[i] !=-1 || rightChild[i] !=-1)
{
dfs(i,leftChild,rightChild,visited);
break;
}
}
if(all_of(visited.begin(),visited.end(),[](bool a){return a;}) and global)
return true;
return false;
}
};