mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
Merge pull request #235 from LiangDazhu/patch-17
添加 0968.监控二叉树 python版本
This commit is contained in:
@ -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:
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user