Binary Tree Coloring Game - Problem

Two players play a turn-based game on a binary tree. We are given the root of this binary tree, and the number of nodes n in the tree. n is odd, and each node has a distinct value from 1 to n.

Initially, the first player names a value x with 1 <= x <= n, and the second player names a value y with 1 <= y <= n and y != x. The first player colors the node with value x red, and the second player colors the node with value y blue.

Then, the players take turns starting with the first player. In each turn, that player chooses a node of their color and colors an uncolored neighbor (either the left child, right child, or parent). If a player cannot choose such a node, they must pass their turn. If both players pass, the game ends, and the winner is the player that colored more nodes.

You are the second player. If it is possible to choose such a y to ensure you win the game, return true. If it is not possible, return false.

Input & Output

Example 1 — Blocking Strategy Works
$ Input: root = [1,2,3,4,5,6,7,8,9,10,11], n = 11, x = 3
Output: true
💡 Note: First player chooses node 3. Second player can choose node 1 to block access to the parent subtree, securing more territory.
Example 2 — No Winning Strategy
$ Input: root = [1,2,3], n = 3, x = 2
Output: false
💡 Note: With only 3 nodes total, first player at node 2 can spread to both children. No position gives second player majority.
Example 3 — Perfect Balance
$ Input: root = [1,2,3,4,5], n = 5, x = 1
Output: true
💡 Note: First player at root (node 1) creates two subtrees of size 2 each. Second player can block one subtree and win 3-2.

Constraints

  • 1 ≤ n ≤ 500
  • n is odd
  • 1 ≤ x ≤ n
  • Tree has exactly n nodes with values 1 to n

Visualization

Tap to expand
Binary Tree Coloring Game INPUT 1 2 3 x (Red) 4 5 6 7 8 9 10 11 n = 11, x = 3 [1,2,3,4,5,6,7,8,9,10,11] Player 1 chose x=3 ALGORITHM STEPS 1 Identify Regions Node x=3 divides tree into 3 regions: left, right, parent 2 Count Subtrees Left of x=3: nodes 6 (1 node) Right of x=3: nodes 7 (1 node) 3 Count Parent Region Parent region of x=3: n - 1 - left - right = 11-1-1-1 = 8 4 Find Best Y Check if max region > n/2 max(1, 1, 8) = 8 > 5.5 Region Sizes Left 1 Right 1 Parent 8 Best! FINAL RESULT Choose y = 2 (parent of x) 1 2 y (Blue) 3 x (Red) Blue Controls Nodes: 1,2,4,5,8,9,10,11 Total: 8 nodes 8 > 11/2 = WIN! Red Controls Nodes: 3,6,7 Total: 3 nodes Output: true Player 2 wins! Key Insight: Strategic Position Analysis When Player 1 picks node x, it divides the tree into 3 disjoint regions: left subtree, right subtree, and parent region. Player 2 wins by choosing y adjacent to x that gives access to the largest region. If max region > n/2, Player 2 wins. Here, y=2 (parent of x=3) gives Blue control of 8 nodes out of 11, guaranteeing victory! TutorialsPoint - Binary Tree Coloring Game | Strategic Position Analysis
Asked in
Google 12 Facebook 8 Amazon 6
28.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen