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

def inorder(root,ans):
    if root== None:
        return 
    if len(root.children) == 0:
        return 
    for i in root.children:
        inorder(i,ans)
    cur_ans =[]
    for i in root.children:
        if i.ch != root.ch:
            cur_ans.append(i.length)
        else:
            cur_ans.append(0)

            # cur_ans = max(cur_ans,i.length + 1)
    cur_ans.sort()
    if len(cur_ans)==1:
        root.length = cur_ans[0]+1
        ans[0]=max(ans[0],root.length)
        return
    ans[0]=max(ans[0],cur_ans[-1]+cur_ans[-2]+1)
    root.length = cur_ans[-1] + 1
    # if len(cur_ans)>1:
    #     ans[0]=max(ans[0],cur_ans[-1]+cur_ans[-2]+1)
    # else:
    #     ans[0]=max(ans[0],root.length)
   


class Node:
    def __init__(self,val,ch):
        self.val = val
        self.ch = ch
        self.children = set()
        self.length = 1

class Solution:
    def longestPath(self, parent: List[int], s: str) -> int:
        l=[]
        for i in range(len(parent)):
            l.append(Node(i,s[i]))
        for i in range(1,len(parent)):
            cur_parent = l[parent[i]]
            curr = l[i]
            cur_parent.children.add(curr)
        res = [1]
        inorder(l[0],res)
        return res[0]
        # root = l[0]
        # ans = []
        # if len(root.children) <=1:
        #     return root.length
        # for i in root.children:
        #     if i.ch != root.ch:
        #         ans.append(i.length)
        # ans.sort()
        # print(ans)
        # if len(ans)>=2:
        #     return root.length + ans[-2]
        # else:
        #     return root.length

    

        
1540