colorBlindCoder's blog

By colorBlindCoder, history, 9 months ago, In English

Hey folks

My friend shared a question asked to him in a MAANG interview:

You are given a Binary tree, each of the nodes of the tree is coloured with a colour belonging to {R, G, B}. You have to find all possible root nodes of the tree s.t. any path from the root to any node follows the colour pattern of R -> G -> B -> R -> G -> B -> R -> .... It need not start from R, it can start from any colour but needs to follow the RGB pattern cyclically.

My Solution:

My first observation is that: there can only be a single node that can be a root of such a tree. We can prove this by saying if there are two such nodes (let's say A and B), then the path from A to B follows the RGB pattern as well as the path from B to A, which is not possible; hence, there can only be a single root following the constraints.

My other observation is: If we choose some other node as root (than the one that follows the constraints), if we go down; path along one of the child will follow RGB pattern and the one along other child (this one has our answer root in its subtree) will follow BGR pattern (basically reverse of RGB). So the one that follows the BGR pattern will at some point reach our actual root node, and from that point onwards, the BGR pattern reverses to the RGB pattern. So basically, we look for this reversal point and this will only be our candidate answer.

Hence, we just verify if our candidate follows the constraints.

I'm not able to find this problem anywhere online. I found the problem quite interesting and hence am sharing here. But more importantly, I want to know if my solution is correct or not?

UPDATE:

kr25161 suggested an alternative solution, which I like even more:

"simultaneously you can say, if any node u is S[i] and all of its neigbours are S[(i + 1) % 3] where S = "RGB", you can check for u if it satifies in O(n). if it doesnt you return false instantly after the check. because any node apart from u cannot be root for this tree."

  • Vote: I like it
  • +49
  • Vote: I do not like it

»
9 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

nice problem. your solution sounds reasonable.

»
9 months ago, hide # |
 
Vote: I like it -15 Vote: I do not like it

i think your solution are correct. But i think that all the edge's direction are fixed in the begining, so a topological order can easily solve this problem.

»
9 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

The solution should work on any tree, not just binary tree

»
9 months ago, hide # |
Rev. 3  
Vote: I like it +11 Vote: I do not like it

simultaneously you can say, if any node u is S[i] and all of its neigbours are S[(i + 1) % 3] where S = "RGB", you can check for u if it satifies in O(n). if it doesnt you return false instantly after the check. because any node apart from u cannot be root for this tree.

  • »
    »
    9 months ago, hide # ^ |
     
    Vote: I like it +3 Vote: I do not like it

    I like your solution; though same time/space complexity, yours is much easier to implement compared to mine

  • »
    »
    9 months ago, hide # ^ |
     
    Vote: I like it +5 Vote: I do not like it

    Nice idea! You could also find a potential root by starting at the root and repeatedly going to one of the children who has the value S[(i + 2) % 3].

»
9 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

A coloring problem for a colorblind person. P/s I really like the problem.

»
9 months ago, hide # |
Rev. 2  
Vote: I like it +6 Vote: I do not like it

Your ideas are nice, but I like doing a Reroot-tree dp in $$$O(n)$$$ without ever engaging with the problem

»
9 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

How were you able to solve this if you are a colorBlindCoder

»
9 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

what do you mean by "there can be only one node that follows such a pattern" ?

consider the tree: R-G-B, we can root the tree at any of the nodes

  • »
    »
    9 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    no.. if you root at R pattern should be B — G — R — G — B if you root at G pattern should be R — B — G — B — R so on. (consider the directed paths from root to all other nodes)

»
9 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

2nd solution is so beautiful and elegant