classSolution{public:intminCameraCover(TreeNode*root){vector<int>ans=dfs(root);returnmin(ans[1],ans[2]);}private:// 0 := all nodes below root are covered except root// 1 := all nodes below and including root are covered w/o camera here// 2 := all nodes below and including root are covered w/ camera herevector<int>dfs(TreeNode*root){if(root==nullptr)return{0,0,1000};vector<int>l=dfs(root->left);vector<int>r=dfs(root->right);constints0=l[1]+r[1];constints1=min(l[2]+min(r[1],r[2]),//r[2]+min(l[1],l[2]));constints2=min({l[0],l[1],l[2]})+//min({r[0],r[1],r[2]})+1;return{s0,s1,s2};}};
classSolution{publicintminCameraCover(TreeNoderoot){int[]ans=dfs(root);returnMath.min(ans[1],ans[2]);}// 0 := all nodes below root are covered except root// 1 := all nodes below and including root are covered w/o camera here// 2 := all nodes below and including root are covered w/ camera hereprivateint[]dfs(TreeNoderoot){if(root==null)returnnewint[]{0,0,1000};int[]l=dfs(root.left);int[]r=dfs(root.right);finalints0=l[1]+r[1];finalints1=Math.min(l[2]+Math.min(r[1],r[2]),r[2]+Math.min(l[1],l[2]));finalints2=1+Math.min(l[0],Math.min(l[1],l[2]))+Math.min(r[0],Math.min(r[1],r[2]));returnnewint[]{s0,s1,s2};}}