<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.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    vector<vector<vector<int>>> dp;
    unordered_map<TreeNode*, int> nodeIndex;
    int index = 0;

    int solve(TreeNode* root, bool cam, bool parCam) {
        if (root == nullptr) {
            return cam ? (1 << 20) : 0;
        }

        int idx = nodeIndex[root];
        if (dp[idx][cam][parCam] != -1) {
            return dp[idx][cam][parCam];
        }

        if (root->left == nullptr && root->right == nullptr) {
            if (cam) {
                return dp[idx][cam][parCam] = 1;
            } else {
                return dp[idx][cam][parCam] = parCam ? 0 : (1 << 20);
            }
        }

        if (cam) {
            dp[idx][cam][parCam] = 1 + min(solve(root->left, false, true), solve(root->left, true, true)) +
                                      min(solve(root->right, false, true), solve(root->right, true, true));
        } else {
            if (parCam) {
                dp[idx][cam][parCam] = min(solve(root->left, false, false), solve(root->left, true, false)) +
                                        min(solve(root->right, false, false), solve(root->right, true, false));
            } else {
                int op1 = solve(root->left, true, false) + min(solve(root->right, false, false), solve(root->right, true, false));
                int op2 = solve(root->right, true, false) + min(solve(root->left, false, false), solve(root->left, true, false));
                dp[idx][cam][parCam] = min(op1, op2);
            }
        }

        return dp[idx][cam][parCam];
    }

    void mapNodes(TreeNode* root) {
        if (root == nullptr) return;
        mapNodes(root->left);
        mapNodes(root->right);
        nodeIndex[root] = index++;
    }

public:
    int minCameraCover(TreeNode* root) {
        mapNodes(root);
        dp = vector<vector<vector<int>>>(index, vector<vector<int>>(2, vector<int>(2, -1)));
        return min(solve(root, false, false), solve(root, true, false));
    }
};

26100