diff --git a/problems/0968.监控二叉树.md b/problems/0968.监控二叉树.md index 24695da9..8f1a3fdb 100644 --- a/problems/0968.监控二叉树.md +++ b/problems/0968.监控二叉树.md @@ -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: