Magical Panda Tree

Правка en1, от ultraraze, 2024-10-27 12:53:19

Given a tree, return a node such that if we root the tree from it, it becomes a magical panda tree which is a binary tree with each layer of nodes alternating between: red, black and white; in a regular pattern

ie. After rooting, nodes of each depth should have the same color & the should be alternating like White-Blue-Red-White-Blue .. you get the idea.

N <= 1e5

I've tried to break it up into cases:

No node with degree 3 can be the root If a node has degree two with same colored adjacent nodes, no other node can be the answer, there must be only one such node and we can do a level order traversal to check if it satisfies the coloring properties.

If a node has degree 2 with different colored adjacent nodes it cannot be the root. Not sure what to do with degree 1. Can't see a pattern.

Теги tree, dfs

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en1 Английский ultraraze 2024-10-27 12:53:19 832 Initial revision (published)