Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Binary Tree Coloring Game in Python
The Binary Tree Coloring Game is a two-player strategy game played on a binary tree. Each player colors nodes to control territory, and the winner is determined by who colors more nodes. As the second player, we need to determine if we can guarantee a win by choosing the optimal starting node.
Game Rules
The game works as follows:
- Player 1 chooses node
xand colors it red - Player 2 chooses node
yand colors it blue - Players alternate turns coloring adjacent uncolored nodes
- The game ends when no moves are possible
- Winner is the player who colored more nodes
Strategy Analysis
The key insight is that once Player 1 colors node x, the tree is divided into three regions:
-
Left subtree of node
x -
Right subtree of node
x - Parent region (everything else)
Player 2 can win by controlling the largest region, which requires more than half the total nodes.
Implementation
class TreeNode:
def __init__(self, data, left=None, right=None):
self.data = data
self.left = left
self.right = right
def insert(temp, data):
queue = []
queue.append(temp)
while len(queue):
temp = queue[0]
queue.pop(0)
if not temp.left:
if data is not None:
temp.left = TreeNode(data)
else:
temp.left = TreeNode(0)
break
else:
queue.append(temp.left)
if not temp.right:
if data is not None:
temp.right = TreeNode(data)
else:
temp.right = TreeNode(0)
break
else:
queue.append(temp.right)
def make_tree(elements):
tree = TreeNode(elements[0])
for element in elements[1:]:
insert(tree, element)
return tree
class Solution:
def btreeGameWinningMove(self, root, n, x):
self.left_count = 0
self.right_count = 0
self.solve(root, x)
# Calculate nodes in parent region
parent_count = n - self.left_count - self.right_count - 1
# Find the largest region
max_region = max(self.left_count, self.right_count, parent_count)
# Player 2 wins if they can control more than half
return max_region > n // 2
def solve(self, node, x, is_left=False, is_right=False):
if not node:
return
# Count nodes in left and right subtrees of x
if is_left:
self.left_count += 1
elif is_right:
self.right_count += 1
# When we find node x, mark its children
if node.data == x:
self.solve(node.left, x, True, False)
self.solve(node.right, x, False, True)
else:
self.solve(node.left, x, is_left, is_right)
self.solve(node.right, x, is_left, is_right)
# Example usage
solution = Solution()
root = make_tree([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
result = solution.btreeGameWinningMove(root, 11, 3)
print(result)
True
How It Works
The algorithm counts nodes in three regions created when Player 1 chooses node x:
-
Traverse the tree to find node
x -
Count nodes in left and right subtrees of
x -
Calculate parent region size:
n - left_count - right_count - 1 - Find maximum region among the three
- Player 2 wins if max region > n/2
Example Walkthrough
For the tree with 11 nodes where Player 1 chooses node 3:
- Left subtree of node 3: 3 nodes
- Right subtree of node 3: 3 nodes
- Parent region: 4 nodes
- Maximum region: 4 nodes > 11/2 = 5.5
- Result: Player 2 can win by choosing a node in the parent region
Conclusion
The solution works by analyzing territory control after Player 1's move. Player 2 wins by choosing a node that gives them access to the largest region, ensuring they color more than half the total nodes.
