Skip to content

Commit cd29a91

Browse files
authored
Update 0968.监控二叉树.md
Added python version code
1 parent ff3605a commit cd29a91

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

problems/0968.监控二叉树.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,27 @@ class Solution {
346346

347347

348348
Python:
349-
350-
349+
```python
350+
class Solution:
351+
def minCameraCover(self, root: TreeNode) -> int:
352+
result = 0
353+
def traversal(cur):
354+
nonlocal result
355+
if not cur:
356+
return 2
357+
left = traversal(cur.left)
358+
right = traversal(cur.right)
359+
if left == 2 and right == 2:
360+
return 0
361+
elif left == 0 or right == 0:
362+
result += 1
363+
return 1
364+
elif left == 1 or right == 1:
365+
return 2
366+
else: return -1
367+
if traversal(root) == 0: result += 1
368+
return result
369+
```
351370
Go:
352371

353372

0 commit comments

Comments
 (0)