Участник:DChulkov/Leet3

Материал из DISCOPAL
Перейти к: навигация, поиск


https://leetcode.com/problems/binary-tree-cameras/submissions/

class Solution:
    def minCameraCover(self, root: TreeNode) -> int:
 
        self.ans = 0
        covered = {None}
 
        def dfs(node, par = None):
            if node:
                dfs(node.left, node)
                dfs(node.right, node)
 
                if (par is None and node not in covered or
                        node.left not in covered or node.right not in covered):
                    self.ans += 1
                    covered.update({node, par, node.left, node.right})
 
        dfs(root)
        return self.ans