<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>

# Question
# find the longest path with no repeate character next to each other.
# Does the path has to start from root and or end at a leaf? No
# it could be from leaf to leaf
# is this a binary tree

# Input
# Length of string == number of nodes
# parent denotes the parents of the node of the current levelt

# Solution
# DFS
# create list of children (all nodes of each level) with index represent the current node visiting
# length of children list should be the same with the string
# DFS each node, calculating the longesth path and compare with the global ans

class Solution:
    ans = 1
    def dfs(self, idx) -> int:
        """Find the longesth path starting from this index."""
        if idx >= len(self.children):
            return 1
        
        char = self.s[idx]
        # top 2 longesth path from this index
        len1 = 0
        len2 = 0
        for child in self.children[idx]:
            cur_len = self.dfs(child)
            if char != self.s[child]:
                if len1 < len2:
                    len1 = max(len1, cur_len)
                else:
                    len2 = max(len2, cur_len)

            # calculate the longest path going through this index
            self.ans = max(self.ans, 1 + len1 + len2)
        
        return 1 + max(len1, len2) # length of the longest path rooting from this index


    def longestPath(self, parent: List[int], s: str) -> int:
        
        children = [[] for _ in range(max(parent) + 1)]
        for i, p in enumerate(parent):
            if i == 0:
                continue
            children[p].append(i)

        self.children = children
        self.s = s
        self.dfs(0)
        return self.ans

        
56025