Formatted question description: https://leetcode.ca/all/1145.html

# 1145. Binary Tree Coloring Game

Medium

## Description

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 (red if player 1, blue if player 2) and colors an uncolored neighbor of the chosen node (either the left child, right child, or parent of the chosen node.)

If (and only if) a player cannot choose such a node in this way, they must pass their turn. If both players pass their turn, 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.

Example 1: Input: root = [1,2,3,4,5,6,7,8,9,10,11], n = 11, x = 3

Output: true

Explanation: The second player can choose the node with value 2.

Constraints:

• root is the root of a binary tree with n nodes and distinct node values from 1 to n.
• n is odd.
• 1 <= x <= n <= 100

## Solution

Define a method to count the number of nodes in a subtree when given the root of the subtree. This can be done using breadth first search.

Consider two cases.

1. The root node has value x. Then obtain the two children of the root and count the number of nodes in both subtrees such that the roots are the two children respectively. If a child is null, then the number of nodes in the subtree is 0. If both subtrees have the same number of nodes, then return false since the first player can always color one of the entire subtrees. If the two subtrees have different numbers of nodes, then return true since the second player can always color the root of the subtree with more nodes.

2. The node with value x is not the root. Find the node, and it has at most three neighbors, which are its left child, its right child and its parent. Count the number of nodes in both subtrees of the node, and count the number of remaining nodes after removing the current subtree (with the current node as the root). After obtaining the numbers of nodes in the three parts, check whether there exists one part that have nodes greater than n / 2. If so, the second player can just color the corresponding neighbor and guarantee to win the game, so return true. Otherwise, the second player can never color more than n / 2 nodes no matter which node is colored by the second player (since the total numbers of nodes in one part is not more than than n / 2), so return false.

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean btreeGameWinningMove(TreeNode root, int n, int x) {
int half = n / 2;
if (root.val == x) {
TreeNode left = root.left, right = root.right;
if (left == null || right == null)
return true;
int count = countNodes(left);
if (count == half)
return false;
else
return true;
} else {
TreeNode xNode = null;
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
if (node.val == x) {
xNode = node;
break;
} else {
TreeNode left = node.left, right = node.right;
if (left != null)
queue.offer(left);
if (right != null)
queue.offer(right);
}
}
int leftCount = 0, rightCount = 0;
TreeNode xLeft = xNode.left, xRight = xNode.right;
if (xLeft != null)
leftCount = countNodes(xLeft);
if (xRight != null)
rightCount = countNodes(xRight);
int remainCount = n - 1 - leftCount - rightCount;
if (leftCount > half || rightCount > half || remainCount > half)
return true;
else
return false;
}
}

public int countNodes(TreeNode root) {
int count = 0;
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
count++;
TreeNode left = node.left, right = node.right;
if (left != null)
queue.offer(left);
if (right != null)
queue.offer(right);
}
return count;
}
}