Skip to content

Commit fc4e5ac

Browse files
authored
Create binary-tree-coloring-game.py
1 parent 8268758 commit fc4e5ac

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Python/binary-tree-coloring-game.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Time: O(n)
2+
# Space: O(h)
3+
4+
# Definition for a binary tree node.
5+
class TreeNode(object):
6+
def __init__(self, x):
7+
self.val = x
8+
self.left = None
9+
self.right = None
10+
11+
12+
class Solution(object):
13+
def btreeGameWinningMove(self, root, n, x):
14+
"""
15+
:type root: TreeNode
16+
:type n: int
17+
:type x: int
18+
:rtype: bool
19+
"""
20+
def count(node, x, left_right):
21+
if not node:
22+
return 0
23+
left, right = count(node.left, x, left_right), count(node.right, x, left_right)
24+
if node.val == x:
25+
left_right[0], left_right[1] = left, right
26+
return left + right + 1
27+
28+
left_right = [0, 0]
29+
count(root, x, left_right)
30+
blue = max(max(left_right), n-(sum(left_right)+1))
31+
return blue > n-blue

0 commit comments

Comments
 (0)