<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 {
public:
    int mx=1e9;
    vector<int> recur(TreeNode* root){
        // returns tuple of size 3
        // res[0]--> this node not monitored by any means but subtrees are monitored
        // res[1]--> this node has camera and this complete subtree is monitored
        // res[2]--> this node do not have camera but complete subtree is monitored
        if(root==NULL) return {mx,mx,0};
        vector<int> v1=recur(root->left);
        vector<int> v2=recur(root->right);
        vector<int> v(3);
        // calculating v[0]
        v[0]=v1[2]+v2[2];

        // v[1]
        int mn1=mx,mn2=mx;
        for(int i=0;i<3;i++){
            mn1=min(mn1,v1[i]);
            mn2=min(mn2,v2[i]);
        }
        v[1]=1+mn1+mn2;

        mn1=min(v1[1],v1[2]);
        mn2=min(v2[1],v2[2]);
        // v[2]
        v[2]=min(v1[1]+mn2,v2[1]+mn1);

        return v;

    }
    int minCameraCover(TreeNode* root) {
        // minimum camera required to monitor the complete binary tree
        vector<int> v=recur(root);
        return min(v[1],v[2]);
    }
};
27200