C++ count nodes in x subtree
Intuition\nAfter the first player choose x node, the best options for the second player are:\n- Choose the parent of x. Second player will color all nodes outsi
# Intuition\nAfter the first player choose ```x``` node, the best options for the second player are:\n- Choose the parent of ```x```. Second player will color all nodes outside ```x``` subtree.\n- Choose the left child of ```x```. Second player will color all nodes in the left child.\n- ... or the right child.\n# Solution\nCount the number of nodes in the left and right subtree of ```x```, and compare to ```n``` to determine if the second player can color more nodes.\n```\nint x_l = 0, x_r = 0;\nint count(TreeNode* root, int x) {\n  if (root == nullptr) return 0;\n  auto l = count(root->left, x), r = count(root->right, x);\n  if (root->val == x) x_l = l, x_r = r;\n  return 1 + l + r;\n}\nbool btreeGameWinningMove(TreeNode* r, int n, int x) {\n  count(r, x);\n  return x_r + x_l < n / 2 || x_l > n / 2 || x_r > n / 2;\n}\n```\n# Complexity Analysis\nRuntime: O(n)\nMemory: O(h), where h is the height of the tree (for the recursion).\n\t
[]