forked from adamespii/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem_160.js
88 lines (72 loc) Β· 1.67 KB
/
problem_160.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* eslint max-classes-per-file: ["error", 2] */
class Edge {
constructor(node, weight) {
this.node = node;
this.weight = weight;
}
}
class Node {
constructor(value) {
this.val = value;
this.edges = [];
}
addEdge(node, weight) {
this.edges.push(new Edge(node, weight));
node.edges.push(new Edge(this, weight));
}
}
/**
* Compute the length of the longest path in the tree.
* @param {Graph} graph
* @return {number}
*/
function longestTreePath(root) {
let maxWeight = 0;
let maxPath = '';
let endNode = null;
let visited = {};
// modified DFS
const dfs = (node, currentPath = '', currentWeight = 0) => {
if (node.edges.length <= 1 && visited[node.val]) {
if (currentWeight > maxWeight) {
maxPath = currentPath + node.val;
maxWeight = currentWeight;
endNode = node;
}
return { maxPath, maxWeight, endNode };
}
node.edges.forEach(edge => {
if (!visited[edge.node.val]) {
visited[edge.node.val] = true;
dfs(
edge.node,
`${currentPath + node.val }->`,
currentWeight + edge.weight
);
}
});
return { maxPath, maxWeight, endNode };
};
// run dfs on root node
dfs(root);
visited = {};
// run dfs on end node
dfs(endNode);
return { maxPath, maxWeight };
}
const A = new Node('a');
const B = new Node('b');
const C = new Node('c');
const D = new Node('d');
const E = new Node('e');
const F = new Node('f');
const G = new Node('g');
const H = new Node('h');
A.addEdge(B, 3);
A.addEdge(C, 5);
A.addEdge(D, 8);
D.addEdge(E, 2);
D.addEdge(F, 4);
E.addEdge(G, 1);
E.addEdge(H, 1);
console.log(longestTreePath(A));