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.
The second player can win if they can control more than half of the nodes by picking a node in the left or right subtree of the node chosen by the first player, or the rest of the tree. We use DFS to count the size of the left and right subtrees of the node with value x.
structTreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
classSolution {
public:int l =0, r =0;
intdfs(TreeNode* node, int x) {
if (!node) return0;
int left = dfs(node->left, x);
int right = dfs(node->right, x);
if (node->val == x) { l = left; r = right; }
return left + right +1;
}
boolbtreeGameWinningMove(TreeNode* root, int n, int x) {
dfs(root, x);
int rest = n - (l + r +1);
return max({l, r, rest}) > n /2;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
typeTreeNodestruct {
ValintLeft, Right*TreeNode}
funcbtreeGameWinningMove(root*TreeNode, n, xint) bool {
varl, rintvardfsfunc(*TreeNode) intdfs = func(node*TreeNode) int {
ifnode==nil { return0 }
left:=dfs(node.Left)
right:=dfs(node.Right)
ifnode.Val==x { l, r = left, right }
returnleft+right+1 }
dfs(root)
rest:=n- (l+r+1)
return max(l, max(r, rest)) > n/2}
func max(a, bint) int { ifa > b { returna }; returnb }
classTreeNode {
int val;
TreeNode left, right;
TreeNode(int x) { val = x; }
}
classSolution {
int l = 0, r = 0;
publicintdfs(TreeNode node, int x) {
if (node ==null) return 0;
int left = dfs(node.left, x);
int right = dfs(node.right, x);
if (node.val== x) { l = left; r = right; }
return left + right + 1;
}
publicbooleanbtreeGameWinningMove(TreeNode root, int n, int x) {
dfs(root, x);
int rest = n - (l + r + 1);
return Math.max(Math.max(l, r), rest) > n / 2;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
dataclassTreeNode(var `val`: Int, var left: TreeNode? = null, var right: TreeNode? = null)
classSolution {
var l = 0var r = 0fundfs(node: TreeNode?, x: Int): Int {
if (node ==null) return0val left = dfs(node.left, x)
val right = dfs(node.right, x)
if (node.`val` == x) { l = left; r = right }
return left + right + 1 }
funbtreeGameWinningMove(root: TreeNode?, n: Int, x: Int): Boolean {
dfs(root, x)
val rest = n - (l + r + 1)
return maxOf(l, r, rest) > n / 2 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classTreeNode:
def__init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
classSolution:
defbtreeGameWinningMove(self, root: TreeNode, n: int, x: int) -> bool:
self.l = self.r =0defdfs(node: TreeNode) -> int:
ifnot node:
return0 left = dfs(node.left)
right = dfs(node.right)
if node.val == x:
self.l, self.r = left, right
return left + right +1 dfs(root)
rest = n - (self.l + self.r +1)
return max(self.l, self.r, rest) > n //2