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

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 (3)