Binary Tree Coloring Game - Problem

Imagine a strategic game played on a binary tree where two players compete to control the most nodes! ๐ŸŽฎ

Game Rules:

  • Two players take turns coloring nodes on a binary tree with n nodes (where n is odd)
  • Each node has a unique value from 1 to n
  • Player 1 (Red) chooses any node x and colors it red
  • Player 2 (Blue) then chooses a different node y and colors it blue
  • Players alternate turns, each coloring an uncolored neighbor (parent, left child, or right child) of their already colored nodes
  • If a player cannot make a move, they pass
  • Game ends when both players pass consecutively
  • Winner: Player who colored more nodes!

Your Mission: You are Player 2. Given the tree structure and Player 1's choice x, determine if you can choose a node y that guarantees your victory. Return true if you can win, false otherwise.

Input & Output

example_1.py โ€” Small Tree Victory
$ Input: root = [1,2,3,4,5,6,7,8,9,10,11], n = 11, x = 3
โ€บ Output: true
๐Ÿ’ก Note: Player 1 chooses node 3. This creates three regions: left subtree (nodes 6,7), right subtree (nodes 12,13 - but these don't exist in our tree, so 0 nodes), and parent region (remaining 8 nodes). Player 2 can choose node 1 to block access to the large parent region containing 8 nodes, guaranteeing victory.
example_2.py โ€” Balanced Tree
$ Input: root = [1,2,3], n = 3, x = 1
โ€บ Output: false
๐Ÿ’ก Note: Player 1 chooses the root node 1. This creates three regions: left subtree (node 2 = 1 node), right subtree (node 3 = 1 node), and parent region (0 nodes since root has no parent). The largest region has only 1 node, which is not greater than 3/2 = 1.5, so Player 2 cannot guarantee a win.
example_3.py โ€” Large Subtree
$ Input: root = [1,2,3,4,5], n = 5, x = 2
โ€บ Output: true
๐Ÿ’ก Note: Player 1 chooses node 2. This creates: left subtree (nodes 4,5 = 2 nodes), right subtree (0 nodes), and parent region (nodes 1,3 = 2 nodes). The largest regions have 2 nodes each, and 2 > 5/2 = 2.5 is false, but Player 2 can still win by strategic placement.

Visualization

Tap to expand
๐Ÿฐ Binary Tree Territory Control GameXRed FortressLeft Territory3 nodesRight Territory2 nodesParent Territory6 nodes - LARGEST!YBlue Strategy๐ŸŽฏ Blue blocks access to largest territory (6 > 11/2) โ†’ Victory!
Understanding the Visualization
1
Player 1 Claims Base
Red army establishes their fortress at node x, creating natural boundaries
2
Territory Analysis
The battlefield divides into 3 distinct regions based on the red fortress location
3
Strategic Positioning
Blue army identifies the largest territory and positions to cut off red's access
4
Victory Condition
If blue can control access to a territory > 50% of total land, victory is guaranteed
Key Takeaway
๐ŸŽฏ Key Insight: Player 2 wins by recognizing that Player 1's choice creates exactly 3 territories, and strategically blocking access to any territory containing more than half the total nodes guarantees victory!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single DFS traversal to find node x and calculate subtree sizes

n
2n
โœ“ Linear Growth
Space Complexity
O(h)

Recursion stack space where h is the height of the tree

n
2n
โœ“ Linear Space

Constraints

  • The number of nodes in the tree is n
  • 1 โ‰ค n โ‰ค 104
  • n is odd
  • 1 โ‰ค Node.val โ‰ค n
  • All Node.val are unique
  • 1 โ‰ค x โ‰ค n
  • Player 1's choice x corresponds to an existing node in the tree
Asked in
Google 45 Meta 32 Amazon 28 Microsoft 18
42.5K Views
Medium Frequency
~15 min Avg. Time
1.8K 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