-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem-1145.cpp
35 lines (31 loc) · 893 Bytes
/
Problem-1145.cpp
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
29
30
31
32
33
34
35
//Problem - 1145
// https://leetcode.com/problems/binary-tree-coloring-game/
class Solution {
public:
int count(TreeNode* root) {
if(!root)
return 0;
return count(root->left) + count(root->right) + 1;
}
TreeNode* find(TreeNode* root, int x) {
if(!root)
return NULL;
if(root->val == x)
return root;
TreeNode* lv = find(root->left, x);
TreeNode* rv = find(root->right, x);
return lv ? lv : rv;
}
bool btreeGameWinningMove(TreeNode* root, int n, int x) {
if(n == 1)
return false;
TreeNode* temp = find(root, x);
int lv = count(temp->left);
int rv = count(temp->right);
int par = n - lv - rv - 1;
int mx = max(lv, max(rv, par));
if(mx > n - mx)
return true;
return false;
}
};