leetcode 1145 - Binary Tree Coloring Game

https://leetcode.com/problems/binary-tree-coloring-game/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public:
bool btreeGameWinningMove(TreeNode* root, int n, int x) {
TreeNode *node = find(root, x);
int left = count(node->left);
int right = count(node->right);
int parent = n - (left + right + 1);

if (left > right + parent + 1 || right > left + parent + 1 || parent > left + right + 1)
return true;
return false;
}

TreeNode *find(TreeNode *root, int x) {
if (!root || root->val == x) return root;

TreeNode *found = find(root->left, x);
if (!found)
found = find(root->right, x);
return found;
}

int count(TreeNode *root) {
if (!root) return 0;

return count(root->left) + count(root->right) + 1;
}
};