Binary Tree Cameras LeetCode solution in JavaScript
1var minCameraCover = function (root) {2let cam = 0;3// 0 --> No covered4// 1 --> covered by camera5// 2 --> has camera6function dfs(root) {7if (root === null) return 1;89const left = dfs(root.left);10const right = dfs(root.right);1112if (left === 0 || right === 0) {13// child required a camera to covered14cam++;15return 2;16} else if (left === 2 || right === 2) {17// child has camera so i am covered18return 1;19} else {20// child is covered but don't have camera,So i want camera21return 0;22}23}2425const ans = dfs(root);2627if (ans === 0) ++cam;2829return cam;30};