-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinaryTreeColoringGame.java
More file actions
50 lines (46 loc) · 1.46 KB
/
BinaryTreeColoringGame.java
File metadata and controls
50 lines (46 loc) · 1.46 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
*
*/
public class BinaryTreeColoringGame {
class Solution {
/**
* Intuition
* The first player colors a node,
* there are at most 3 nodes connected to this node.
* Its left, its right and its parent.
* Take this 3 nodes as the root of 3 subtrees.
*
* The second player just color any one root,
* and the whole subtree will be his.
* And this is also all he can take,
* since he cannot cross the node of the first player.
*
*
* Explanation
* count will recursively count the number of nodes,
* in the left and in the right.
* n - left - right will be the number of nodes in the "subtree" of parent.
* Now we just need to compare max(left, right, parent) and n / 2
*
*
* Complexity
* Time O(N)
* Space O(height) for recursion
*/
int left, right, val;
public boolean btreeGameWinningMove(TreeNode root, int n, int x) {
val = x;
count(root);
return Math.max(Math.max(left, right), n - left - right - 1) > n / 2;
}
private int count(TreeNode node) {
if (node == null) return 0;
int l = count(node.left), r = count(node.right);
if (node.val == val) {
left = l;
right = r;
}
return l + r + 1;
}
}
}