-
Notifications
You must be signed in to change notification settings - Fork 0
/
968.监控二叉树.js
59 lines (56 loc) · 1.3 KB
/
968.监控二叉树.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* @lc app=leetcode.cn id=968 lang=javascript
*
* [968] 监控二叉树
*/
// @lc code=start
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
const getDp = (root, dp) => {
if (!root) {
dp[0][0] = 0;
dp[0][1] = 10000;
dp[1][0] = 0;
dp[1][1] = 10000;
return;
}
if (!root.left && !root.rught) {
dp[0][0] = 10000;
dp[1][0] = 0;
dp[1][1] = 1;
dp[0][1] = 1;
return;
}
const l = [[], []],
r = [[], []];
getDp(root.left, l);
getDp(root.right, r);
dp[0][0] = Math.min(
Math.min(l[0][1] + r[0][0], l[0][0] + r[0][1]),
l[0][1] + r[0][1]
);
dp[1][0] = Math.min(dp[0][0], l[0][0] + r[0][0]);
dp[0][1] =
Math.min(
Math.min(l[1][0] + r[1][0], l[1][1] + r[1][1]),
Math.min(l[1][0] + r[1][1], l[1][1] + r[1][0])
) + 1;
dp[1][1] = dp[0][1];
return;
};
/**
* @param {TreeNode} root
* @return {number}
*/
var minCameraCover = function (root) {
const dp = [[], []];
getDp(root, dp);
return Math.min(dp[0][1], dp[0][0]);
};
// @lc code=end