-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path137278
48 lines (30 loc) · 832 Bytes
/
137278
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
var strArr = ["(1,2)", "(2,4)", "(5,7)", "(7,2)" ,"(9,5)"];
var strArr1= ["(1,2)", "(3,2)", "(2,12)", "(5,2)"];
function TreeConstructor(strArr) {
var parents = {};
for (var i = 0; i < strArr.length; i++) {
var pair = strArr[i].slice(1, -1).split(",");
var fil = pair[0];
var pere = pair[1];
if (fil in parents) {
return false;
}
parents[fil] = pere;
}
for (var fil in parents) {
var pere = parents[fil];
var count = 0;
for (var otherfil in parents) {
if (parents[otherfil] === pere) {
count++;
}
}
if (count > 2) {
return false;
}
}
return true;
}
console.log(TreeConstructor(strArr));
console.log(TreeConstructor(strArr1));
module.exports = TreeConstructor;