<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 {
    int rec(TreeNode* root,int x,map<pair<TreeNode*,int>,int>&dp) {
        if(root==NULL){
            return 0;
        }
        if(dp[{root,x}]!=-1){
            return dp[{root,x}];
        }
        int ans=0;
        if(x==-1){
            int ans1=1 + rec(root->left,-1,dp) + rec(root->right,-1,dp);
            int ans2=rec(root->left,0,dp) + rec(root->right,0,dp);
            return dp[{root,x}]=min(ans1,ans2);
        }
        if(x==1){
            return dp[{root,x}]=1 + rec(root->left,-1,dp) + rec(root->right,-1,dp);
        }
        if(root->left==NULL and root->right==NULL){
            return dp[{root,x}]=1;
        }
        int ans1=1 + rec(root->left,-1,dp) + rec(root->right,-1,dp);
        if(root->right==NULL){
            return dp[{root,x}]=min(ans1,rec(root->left,1,dp));
        }
        if(root->left==NULL){
            return dp[{root,x}]=min(ans1,rec(root->right,1,dp));
        }
        int ans2=rec(root->left,0,dp) + rec(root->right,1,dp);
        int ans3=rec(root->left,1,dp) + rec(root->right,0,dp);
        return dp[{root,x}]=min(ans1,min(ans2,ans3));
    }
    void in(vector<TreeNode*>&v,TreeNode* root){
        if(root==NULL){
            return;
        }
        in(v,root->left);
        v.push_back(root);
        in(v,root->right);
    }
public:
    int minCameraCover(TreeNode* root){
        map<pair<TreeNode*,int>,int>dp;
        vector<TreeNode*>v;
        in(v,root);
        for(int i=0;i<v.size();i++){
           dp[{v[i],-1}]=-1;
           dp[{v[i],0}]=-1;
           dp[{v[i],1}]=-1;
        }
        return rec(root,0,dp);
    }
};
26600