Longest Univalue path

Longest Univalue path_第1张图片

这题有一点像之前做过的longest path题的变形。

使用一个Global Variable 来track longest among tree.

比较Tricky的是ans = Math.max(ans, arrLeft+arrRight);

和return Math.max(arrowLeft, arrowRight)

longest seen so far 存在ans里,我们还是要返回一个arrowLeft or arrowRight so that its parent node

might be able to merge it to get a even longer path.

Longest Univalue path_第2张图片

你可能感兴趣的:(Longest Univalue path)