-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryTreeCameras.py
More file actions
40 lines (34 loc) · 1.01 KB
/
binaryTreeCameras.py
File metadata and controls
40 lines (34 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/binary-tree-cameras/
# Author: Miao Zhang
# Date: 2021-03-29
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
from enum import Enum, unique
@unique
class State(Enum):
NONE = 1
COVERED = 2
CAMERA = 3
class Solution:
def __init__(self):
self.res = 0
def minCameraCover(self, root: TreeNode) -> int:
if self.dfs(root) == State.NONE:
self.res += 1
return self.res
def dfs(self, root: TreeNode) -> 'State':
if not root: return State.COVERED
l = self.dfs(root.left)
r = self.dfs(root.right)
if l == State.NONE or r == State.NONE:
self.res += 1
return State.CAMERA
if l == State.CAMERA or r == State.CAMERA:
return State.COVERED
return State.NONE