Skip to content

Commit 6ef1797

Browse files
author
shixuan.ma
committed
https://leetcode.cn/problems/binary-tree-cameras
1 parent 813fa81 commit 6ef1797

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ go get github.com/masx200/leetcode-test
152152

153153
<summary>展开查看</summary>
154154

155+
https://leetcode.cn/problems/binary-tree-cameras
156+
155157
https://leetcode.cn/problems/snail-traversal/
156158

157159
https://leetcode.cn/problems/function-composition

binary-tree-cameras/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
2+
3+
function minCameraCover(root: TreeNode | null): number {
4+
const dfs = (root: TreeNode | null): [number, number, number] => {
5+
if (!root) {
6+
return [Math.floor(Number.MAX_SAFE_INTEGER / 2), 0, 0];
7+
}
8+
const [la, lb, lc] = dfs(root.left);
9+
const [ra, rb, rc] = dfs(root.right);
10+
const a = lc + rc + 1;
11+
const b = Math.min(a, Math.min(la + rb, ra + lb));
12+
const c = Math.min(a, lb + rb);
13+
return [a, b, c];
14+
};
15+
16+
return dfs(root)[1];
17+
}
18+
export default minCameraCover;

0 commit comments

Comments
 (0)