Binary Tree Coloring Game
specialist · 840 · lc medium +31 · verified · 52.9% accepted · 1,402 likes · top 43%
Description
Two opponents compete in a turn-based game on a binary tree. You are given the root and the total node count n (which is odd), where nodes hold distinct values from 1 to n.
To begin, the first player picks value x (1 <= x <= n), and the second player picks a different value y (1 <= y <= n, y != x). The first player's node is colored red; the second player's is colored blue.
Players alternate turns starting with the first. On each turn, a player selects one of their colored nodes and colors an adjacent uncolored neighbor (left child, right child, or parent) in their color. A player passes when no such move exists. The game ends when both players pass consecutively, and whoever has colored more nodes wins.
You are the second player. Return true if there exists a choice of y that guarantees your victory, otherwise return false.
Example 1:
Example 2:
Code