<p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size <code>n</code>, where <code>parent[i]</code> is the parent of node <code>i</code>. Since node <code>0</code> is the root, <code>parent[0] == -1</code>.</p>

<p>You are also given a string <code>s</code> of length <code>n</code>, where <code>s[i]</code> is the character assigned to node <code>i</code>.</p>

<p>Return <em>the length of the <strong>longest path</strong> in the tree such that no pair of <strong>adjacent</strong> nodes on the path have the same character assigned to them.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/25/testingdrawio.png" style="width: 201px; height: 241px;" />
<pre>
<strong>Input:</strong> parent = [-1,0,0,1,1,2], s = &quot;abacbe&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> The longest path where each two adjacent nodes have different characters in the tree is the path: 0 -&gt; 1 -&gt; 3. The length of this path is 3, so 3 is returned.
It can be proven that there is no longer path that satisfies the conditions. 
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/25/graph2drawio.png" style="width: 201px; height: 221px;" />
<pre>
<strong>Input:</strong> parent = [-1,0,0,0], s = &quot;aabc&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> The longest path where each two adjacent nodes have different characters is the path: 2 -&gt; 0 -&gt; 3. The length of this path is 3, so 3 is returned.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == parent.length == s.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= parent[i] &lt;= n - 1</code> for all <code>i &gt;= 1</code></li>
	<li><code>parent[0] == -1</code></li>
	<li><code>parent</code> represents a valid tree.</li>
	<li><code>s</code> consists of only lowercase English letters.</li>
</ul>

class TreeNode:
    def __init__(self, val):
        self.val = val
        self.children = dict()  # node id to node

class Solution:
    def _longest_helper(self, curr):
        # returns child_longest_to_root, child_2nd_longest_to_root, subtree_longest
        if not curr:
            return 0, 0, 0
        elif not curr.children:
            return 1, 0, 1  # the longest is curr itself, 2nd longest is None

        # initialize assuming child and curr val are the same
        longest1, longest2, l1node, l2node = 1, 0, curr, None
        curr_max = 1
        for child in curr.children.values():
            cl1, cl2, child_max = self._longest_helper(child)
            curr_max = max(curr_max, child_max)
            if child.val != curr.val:
                # because cl1 and cl2 come from the same child, we can only choose one path
                # to propagate the current level, hence we choose the longest cl1
                if cl1+1 > longest1:
                    longest2, l2node = longest1, l1node
                    longest1, l1node = cl1+1, curr
                elif cl1+1 > longest2:
                    longest2, l2node = cl1+1, curr

        seg = 1
        if l1node:
            seg += longest1-1
        if l2node:
            seg += longest2-1
        curr_max = max(curr_max, seg)

        return longest1, longest2, curr_max


    def longestPath(self, parent: List[int], s: str) -> int:
        nodes = [TreeNode(s[i]) for i in range(len(parent))]
        for i in range(1, len(parent)):
            nodes[parent[i]].children[i] = nodes[i]  # do not use list append, it recursively deepcopy

        a, b, c = self._longest_helper(nodes[0])
        return max(a, b, c)

1382