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
nnodes (wherenis odd) - Each node has a unique value from
1ton - Player 1 (Red) chooses any node
xand colors it red - Player 2 (Blue) then chooses a different node
yand 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
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
โ Linear Growth
Space Complexity
O(h)
Recursion stack space where h is the height of the tree
โ Linear Space
Constraints
-
The number of nodes in the tree is
n -
1 โค n โค 104 -
nis 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code