Update 0968.监控二叉树.md

Added python version code
This commit is contained in:
LiangDazhu
2021-05-23 20:21:40 +08:00
committed by GitHub
parent ff3605a52b
commit cd29a919d7

View File

@ -346,8 +346,27 @@ class Solution {
Python
```python
class Solution:
def minCameraCover(self, root: TreeNode) -> int:
result = 0
def traversal(cur):
nonlocal result
if not cur:
return 2
left = traversal(cur.left)
right = traversal(cur.right)
if left == 2 and right == 2:
return 0
elif left == 0 or right == 0:
result += 1
return 1
elif left == 1 or right == 1:
return 2
else: return -1
if traversal(root) == 0: result += 1
return result
```
Go