Find a valid root for a tree-shaped graph.
Unlock detailed company stats for this questionUpgrade
Given a connected acyclic graph where every node has at most 3 neighbours, find a valid node that forms a binary tree as root. Follow-ups, about 10 minutes each: 1) nodes are black or white and colors must alternate at every depth; 2) the graph can be disconnected; 3) nodes are red, black or white and depths must follow the sequence RWB RWB..., root of any color. The final follow-up has an O(n) solution proving only one unique root is possible.
Expect follow-ups like
- Now every node is either black or white, and at every depth in the tree colors must alternate. Find a valid node.
- Now the graph can be disconnected.
- Now every node is red, black or white, and the only allowed depth sequence is RWB RWB..., with the root of any color. Find a valid root.
Format · 45-minute DSA round; solve, code and discuss complexity per follow-up.
Community answers
No answers contributed by the community yet.
Related courses

Course

Course
Course
