From cd29a919d7e83cfbc853380ca2bb250d49d87efe Mon Sep 17 00:00:00 2001 From: LiangDazhu <42199191+LiangDazhu@users.noreply.github.com> Date: Sun, 23 May 2021 20:21:40 +0800 Subject: [PATCH] =?UTF-8?q?Update=200968.=E7=9B=91=E6=8E=A7=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added python version code --- problems/0968.监控二叉树.md | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) 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: