3
// nodex means "node with val = x"4
// Idea behind this is to block either nodex's parent or it's left child or5
// right child. Block means we will chose that node as nodey. Why? because it6
// will devide the tree in two parts, one for player 1 and other for player 2.7
// Then we have to just take the maximum no of nodes we can get, from these8
// three nodes as head, and if max is greater than n/2, means we can will.9
TreeNode *nodex, *parentx;14
int count(TreeNode *root) {15
if (root == NULL) return 0;16
int a = count(root->left);17
int b = count(root->right);20
bool findx(TreeNode *root, int x) {21
if (root == NULL) return false;27
bool a = findx(root->left, x);29
bool b = findx(root->right, x);32
bool btreeGameWinningMove(TreeNode *root, int n, int x) {33
findx(root, x); // Find the node which has val = x34
int p = n - count(nodex); // Count the no of nodes of blue color, if we35
// decide to take parent of nodex as nodey36
int r = count(nodex->right); // Count the no of nodes of blue color, if we37
// decide to take right child of nodex as nodey38
int l = count(nodex->left); // Count the no of nodes of blue color, if we39
// decide to take left child of nodex as nodey41
mx = max(mx, l); // max of all three possible scenarios