<p>You are given the <code>root</code> of a binary tree. We install cameras on the tree nodes where each camera at a node can monitor its parent, itself, and its immediate children.</p>

<p>Return <em>the minimum number of cameras needed to monitor all nodes of the tree</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/29/bst_cameras_01.png" style="width: 138px; height: 163px;" />
<pre>
<strong>Input:</strong> root = [0,0,null,0,0]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One camera is enough to monitor all nodes if placed as shown.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/29/bst_cameras_02.png" style="width: 139px; height: 312px;" />
<pre>
<strong>Input:</strong> root = [0,0,null,0,null,0,null,null,0]
<strong>Output:</strong> 2
<strong>Explanation:</strong> At least two cameras are needed to monitor all nodes of the tree. The above image shows one of the valid configurations of camera placement.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
	<li><code>Node.val == 0</code></li>
</ul>

# 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
# class Solution:
#     def minCameraCover(self, root: Optional[TreeNode]) -> int:
#         result = []
#         if root.left and root.right:
#             result.append(1 + self.traverse(root.left, 1, 1) + self.traverse(root.right, 1, 1))
#             result.append(1 + self.traverse(root.left, 0, 1) + self.traverse(root.right, 0, 1))
#             result.append(1 + self.traverse(root.left, 1, 1) + self.traverse(root.right, 0, 1))
#             result.append(1 + self.traverse(root.left, 0, 1) + self.traverse(root.right, 1, 1))
#             result.append(0 + self.traverse(root.left, 0, 0) + self.traverse(root.right, 1, 0))
#             result.append(0 + self.traverse(root.left, 1, 0) + self.traverse(root.right, 0, 0))
#         elif root.left:
#             result.append(1 + self.traverse(root.left, 1, 1))
#             result.append(1 + self.traverse(root.left, 0, 1))
#             result.append(0 + self.traverse(root.left, 1, 0))
#         elif root.right:
#             result.append(1 + self.traverse(root.right, 1, 1))
#             result.append(1 + self.traverse(root.right, 0, 1))
#             result.append(0 + self.traverse(root.right, 1, 0))
#         else:
#             return 1
#         return min(result)

#     def traverse(self, root: Optional[TreeNode], has_camera: int, is_monitored: int):
#         result = []
#         if root.left and root.right:
#             if has_camera:
#                 result.append(1 + self.traverse(root.left, 1, 1) + self.traverse(root.right, 1, 1))
#                 result.append(1 + self.traverse(root.left, 0, 1) + self.traverse(root.right, 0, 1))
#                 result.append(1 + self.traverse(root.left, 1, 1) + self.traverse(root.right, 0, 1))
#                 result.append(1 + self.traverse(root.left, 0, 1) + self.traverse(root.right, 1, 1))
#             elif is_monitored:
#                 result.append(1 + self.traverse(root.left, 1, 1) + self.traverse(root.right, 1, 1))
#                 result.append(1 + self.traverse(root.left, 0, 1) + self.traverse(root.right, 0, 1))
#                 result.append(1 + self.traverse(root.left, 1, 1) + self.traverse(root.right, 0, 1))
#                 result.append(1 + self.traverse(root.left, 0, 1) + self.traverse(root.right, 1, 1))
#                 result.append(0 + self.traverse(root.left, 1, 0) + self.traverse(root.right, 1, 0))
#                 result.append(0 + self.traverse(root.left, 1, 0) + self.traverse(root.right, 0, 0))
#                 result.append(0 + self.traverse(root.left, 0, 0) + self.traverse(root.right, 1, 0))
#                 result.append(0 + self.traverse(root.left, 0, 0) + self.traverse(root.right, 0, 0))
#             else:
#                 result.append(0 + self.traverse(root.left, 1, 0) + self.traverse(root.right, 1, 0))
#                 result.append(0 + self.traverse(root.left, 1, 0) + self.traverse(root.right, 0, 0))
#                 result.append(0 + self.traverse(root.left, 0, 0) + self.traverse(root.right, 1, 0))
#         elif root.left:
#             if has_camera:
#                 result.append(1 + self.traverse(root.left, 1, 1))
#                 result.append(1 + self.traverse(root.left, 0, 1))
#             elif is_monitored:
#                 result.append(1 + self.traverse(root.left, 1, 1))
#                 result.append(1 + self.traverse(root.left, 0, 1))
#                 result.append(0 + self.traverse(root.left, 1, 0))
#                 result.append(0 + self.traverse(root.left, 0, 0))
#             else:
#                 result.append(0 + self.traverse(root.left, 1, 0))
#         elif root.right:
#             if has_camera:
#                 result.append(1 + self.traverse(root.right, 1, 1))
#                 result.append(1 + self.traverse(root.right, 0, 1))
#             elif is_monitored:
#                 result.append(1 + self.traverse(root.right, 1, 1))
#                 result.append(1 + self.traverse(root.right, 0, 1))
#                 result.append(0 + self.traverse(root.right, 1, 0))
#                 result.append(0 + self.traverse(root.right, 0, 0))
#             else:
#                 result.append(0 + self.traverse(root.right, 1, 0))
#         else:
#             if has_camera:
#                 return 1
#             elif is_monitored:
#                 return 0
#             else:
#                 return 1
#         return min(result)




class Solution:
    def minCameraCover(self, root: Optional[TreeNode]) -> int:
        queue = [root]
        cumulative = 0
        while queue:
            new_queue = []
            names = list(range(cumulative, cumulative+len(queue)))
            cumulative += len(queue)
            while queue:
                node = queue.pop()
                node.val = names.pop()
                if node.left:
                    new_queue.append(node.left)
                if node.right:
                    new_queue.append(node.right)
            queue = new_queue

        self.dp = defaultdict()
        result = []
        if root.left and root.right:
            result.append(1 + self.traverse(root.left, 1, 1) + self.traverse(root.right, 1, 1))
            result.append(1 + self.traverse(root.left, 0, 1) + self.traverse(root.right, 0, 1))
            result.append(1 + self.traverse(root.left, 1, 1) + self.traverse(root.right, 0, 1))
            result.append(1 + self.traverse(root.left, 0, 1) + self.traverse(root.right, 1, 1))
            result.append(0 + self.traverse(root.left, 0, 0) + self.traverse(root.right, 1, 0))
            result.append(0 + self.traverse(root.left, 1, 0) + self.traverse(root.right, 0, 0))
        elif root.left:
            result.append(1 + self.traverse(root.left, 1, 1))
            result.append(1 + self.traverse(root.left, 0, 1))
            result.append(0 + self.traverse(root.left, 1, 0))
        elif root.right:
            result.append(1 + self.traverse(root.right, 1, 1))
            result.append(1 + self.traverse(root.right, 0, 1))
            result.append(0 + self.traverse(root.right, 1, 0))
        else:
            return 1
        return min(result)

    def traverse(self, root: Optional[TreeNode], has_camera: int, is_monitored: int):
        name = f'{root.val}_{has_camera}_{is_monitored}'
        if name in self.dp:
            return self.dp[name]
        result = []
        if root.left and root.right:
            if has_camera:
                result.append(1 + self.traverse(root.left, 1, 1) + self.traverse(root.right, 1, 1))
                result.append(1 + self.traverse(root.left, 0, 1) + self.traverse(root.right, 0, 1))
                result.append(1 + self.traverse(root.left, 1, 1) + self.traverse(root.right, 0, 1))
                result.append(1 + self.traverse(root.left, 0, 1) + self.traverse(root.right, 1, 1))
            elif is_monitored:
                result.append(1 + self.traverse(root.left, 1, 1) + self.traverse(root.right, 1, 1))
                result.append(1 + self.traverse(root.left, 0, 1) + self.traverse(root.right, 0, 1))
                result.append(1 + self.traverse(root.left, 1, 1) + self.traverse(root.right, 0, 1))
                result.append(1 + self.traverse(root.left, 0, 1) + self.traverse(root.right, 1, 1))
                result.append(0 + self.traverse(root.left, 1, 0) + self.traverse(root.right, 1, 0))
                result.append(0 + self.traverse(root.left, 1, 0) + self.traverse(root.right, 0, 0))
                result.append(0 + self.traverse(root.left, 0, 0) + self.traverse(root.right, 1, 0))
                result.append(0 + self.traverse(root.left, 0, 0) + self.traverse(root.right, 0, 0))
            else:
                result.append(0 + self.traverse(root.left, 1, 0) + self.traverse(root.right, 1, 0))
                result.append(0 + self.traverse(root.left, 1, 0) + self.traverse(root.right, 0, 0))
                result.append(0 + self.traverse(root.left, 0, 0) + self.traverse(root.right, 1, 0))
        elif root.left:
            if has_camera:
                result.append(1 + self.traverse(root.left, 1, 1))
                result.append(1 + self.traverse(root.left, 0, 1))
            elif is_monitored:
                result.append(1 + self.traverse(root.left, 1, 1))
                result.append(1 + self.traverse(root.left, 0, 1))
                result.append(0 + self.traverse(root.left, 1, 0))
                result.append(0 + self.traverse(root.left, 0, 0))
            else:
                result.append(0 + self.traverse(root.left, 1, 0))
        elif root.right:
            if has_camera:
                result.append(1 + self.traverse(root.right, 1, 1))
                result.append(1 + self.traverse(root.right, 0, 1))
            elif is_monitored:
                result.append(1 + self.traverse(root.right, 1, 1))
                result.append(1 + self.traverse(root.right, 0, 1))
                result.append(0 + self.traverse(root.right, 1, 0))
                result.append(0 + self.traverse(root.right, 0, 0))
            else:
                result.append(0 + self.traverse(root.right, 1, 0))
        else:
            if has_camera:
                return 1
            elif is_monitored:
                return 0
            else:
                return 1
        self.dp[name] = min(result)
        return self.dp[name]
17400