Skip to content

Commit 5bbdeff

Browse files
authored
Create 968-binary-tree-cameras.js
1 parent c84da80 commit 5bbdeff

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

968-binary-tree-cameras.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {number}
11+
*/
12+
const minCameraCover = function(root) {
13+
if (root === null) return 0;
14+
let max = 0;
15+
return (helper(root) < 1 ? 1 : 0) + max;
16+
function helper(root) {
17+
if (root === null) return 2;
18+
if (root.left === null && root.right === null) return 0;
19+
let left = helper(root.left);
20+
let right = helper(root.right);
21+
if (left === 0 || right === 0) {
22+
max++;
23+
return 1;
24+
}
25+
return left === 1 || right === 1 ? 2 : 0;
26+
}
27+
};

0 commit comments

Comments
 (0)