-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathMinimum Time to Collect All Apples in a Tree.js
41 lines (39 loc) · 1.48 KB
/
Minimum Time to Collect All Apples in a Tree.js
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
var cnvrtAdjLst = (mat) => {//converts the list of edges to adjacency list
let map = new Map();
for(let i=0; i<mat.length; i++){
let [p,c] = mat[i];
if(map[p]===undefined) map[p] = [c];
else map[p].push(c);
if(map[c]===undefined) map[c] = [p];
else map[c].push(p);
}
return map;
}
var minTime = function(n, edges, hasApple) {
let adjList = cnvrtAdjLst(edges);//convert to adjacency list for undirected graph
let visited = new Set();// visited set for checking trivial loop
let dfsForApples = (src, len) => {
visited.add(src);// add root to visited set, as we start exploring the subtree
let lengthFromNow = 0;//total length of path visited by each child, 0 if no child has apple
for(let i in adjList[src]){
if(!visited.has(adjList[src][i])){
lengthFromNow += dfsForApples(adjList[src][i],len+1);// add child path length to total length
}
}
if(lengthFromNow>0){
//if there is an apple present in the subtree
return lengthFromNow + 2;
}
else if(hasApple[src]){
//if there is no apple in subtree but apple is present on the root node
return 2;
}
else{
// if no apple is present in subtree
return 0;
}
}
visited.add(0);
let res = dfsForApples(0,0);
return res?res-2:0;//check if the root node itself has an apple or no apple is present
};