Skip to content

Commit 32e9a6c

Browse files
author
Partho Biswas
committed
1145_Binary_Tree_Coloring_Game
1 parent 092db9e commit 32e9a6c

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ Check this [golden](https://tinyurl.com/ujopecz) post.
334334
|23| [543. Diameter of Binary Tree](https://tinyurl.com/womnj8m) | [Python](https://tinyurl.com/wu6rdaw/543_Diameter_of_Binary_Tree.py)| [Educative.io](https://tinyurl.com/w6rrucg), [Art 1](https://tinyurl.com/vfnvg8q) | Medium | 📌 Important |
335335
|24| **[1110. Delete Nodes And Return Forest](https://tinyurl.com/rg8qomj)** | [Python](https://tinyurl.com/wu6rdaw/1110_Delete_Nodes_And_Return_Forest.py)| [Vid 1](https://tinyurl.com/t9rax3x), [Vid 2](https://tinyurl.com/uw6y3zk), [Art 1](https://tinyurl.com/ujopecz), [Art 2](https://tinyurl.com/tppglsu) | Medium | 📌 **TODO: Check again. Important |
336336
|25| [257. Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/)| [Python](https://tinyurl.com/wu6rdaw/257_Binary_Tree_Paths.py)| --- | Easy | DFS |
337+
|26| **[1145. Binary Tree Coloring Game](https://tinyurl.com/uukf2nm)**| [Python](https://tinyurl.com/wu6rdaw/1145_Binary_Tree_Coloring_Game.py)| [Art 1](https://tinyurl.com/v4k7jaj), [Art 2](https://tinyurl.com/rvwsjar) | Medium | DFS |
337338

338339
</p>
339340
</details>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def findNode(self, nodeVal, root):
10+
if not root:
11+
return None
12+
if root.val == nodeVal:
13+
return root
14+
left = self.findNode(nodeVal, root.left)
15+
if left:
16+
return left
17+
else:
18+
return self.findNode(nodeVal, root.right)
19+
20+
def countNode(self, root):
21+
if not root:
22+
return 0
23+
leftCount = self.countNode(root.left)
24+
rightCount = self.countNode(root.right)
25+
return leftCount + rightCount + 1
26+
27+
def btreeGameWinningMove(self, root, n, x):
28+
"""
29+
:type root: TreeNode
30+
:type n: int
31+
:type x: int
32+
:rtype: bool
33+
"""
34+
xNode = self.findNode(x, root)
35+
xNodeLeftCount = self.countNode(xNode.left)
36+
xNodeRightCount = self.countNode(xNode.right)
37+
xNodeParentCount = n - xNodeLeftCount - xNodeRightCount - 1
38+
return any([xNodeParentCount > xNodeLeftCount + xNodeRightCount,
39+
xNodeLeftCount > xNodeRightCount + xNodeParentCount,
40+
xNodeRightCount > xNodeLeftCount + xNodeParentCount])

0 commit comments

Comments
 (0)