Binary Tree Coloring Game LeetCode solution in Java

1
class Solution {
2
int xkaLeft = 0, xkaRight = 0;
3

4
public int size(TreeNode node, int x) {
5
if (node == null) {
6
return 0;
7
}
8
int ls = size(node.left, x);
9
int rs = size(node.right, x);
10

11
if (node.val == x) {
12
xkaLeft = ls;
13
xkaRight = rs;
14
}
15

16
return ls + rs + 1;
17
}
18

19
public boolean btreeGameWinningMove(TreeNode root, int n, int x) {
20

21
size(root, x);
22
int parent = n - (xkaLeft + xkaRight + 1);
23
int max = Math.max(parent, Math.max(xkaRight, xkaLeft));
24
if (max > n / 2) {
25
return true;
26
}
27
return false;
28
}
29
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0