Microsoft | Q/A Question | Longest path in a tree with alternating characters

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2022-03-03Added By Anonymous
Source: Link

Given an n-ary tree with N nodes numbered 0..N-1. Each node is marked as 'a' or 'b'.

Find longest path in a tree with alternating characters.

Example 1 :

0 'a'

|

1 'b'

|

2 'b'

Path length = 2 (0->1)

Example 2:

0 'a'

/

1 'b' 2'b'

Path length = 2 (1->0->2)

Suggest an optimal solution for this.

Comments