1
var longestPath = function (parent, s) {2
const adjList = parent.reduce(3
(adjList, parent, node) => {4
if (parent < 0) return adjList;5
adjList[parent].push(node);8
new Array(parent.length).fill(0).map(() => [])13
const getLongest = (node) => {17
adjList[node].forEach((child) => {18
const childLength = getLongest(child);20
// child letter matches our node, so we can't use it21
if (s[child] === s[node]) return;23
// compare and update the longest two child paths24
if (childLength > maxChild1) {25
maxChild2 = maxChild1;26
maxChild1 = childLength;27
} else if (childLength > maxChild2) {28
maxChild2 = childLength;33
longest = Math.max(longest, maxChild1 + maxChild2 + 1);35
// return longest path up the tree