Merge pull request #235 from LiangDazhu/patch-17

添加 0968.监控二叉树 python版本
This commit is contained in:
Carl Sun
2021-05-24 18:01:04 +08:00
committed by GitHub

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