-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path968.py
37 lines (26 loc) · 785 Bytes
/
968.py
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
# leetcode 968 监控二叉树
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def __init__(self):
self.res = 0
def minCameraCover(self, root: Optional[TreeNode]) -> int:
if self.travel(root) == 0:
self.res += 1
return self.res
def travel(self, cur):
if not cur:
return 2
left = self.travel(cur.left)
right = self.travel(cur.right)
if left == 2 and right == 2:
return 0
if left == 0 or right == 0:
self.res += 1
return 1
if left == 1 or right == 1:
return 2