<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 lim = 1e8;
    int minCameraCover(TreeNode* root) {
        vector<int> res = backtrack(root);
        return max(1, min(res[0], res[1]));
    }

    vector<int> backtrack(TreeNode* root){
        if(root == nullptr) {
            return {lim, 0, 0};
        }
        if(root->left == nullptr && root->right == nullptr) {
            return {1, lim, 0};
        }

        vector<int> left = backtrack(root->left);
        vector<int> right = backtrack(root->right);
        
        //l1 = including the camera, l2 = excluding the camera but parent has no camera, l3 = excluding the camera but parent has camera.

        //include camera on that note.
        //exclude camera, but given parent has no camera --> you must include camera at least one of child node
        //exclude camera, but given parent has camera --> you either include camera at the child node or not w/ parent node missing.
        int included = 1 + min(min(left[0], left[1]), left[2]) + min(min(right[0], right[1]), right[2]);
        int excludedParentNoCamera = min(
            left[0] + min(right[0], right[1]),
            right[0] + min(left[0], left[1])
        );
        int excludedParentHasCamera = min(left[0], left[1]) + min(right[0], right[1]);  

        // cout << included << " " << excludedParentNoCamera << " " << excludedParentHasCamera << endl;  
        return {included, excludedParentNoCamera, excludedParentHasCamera};
    }
};
25000