8000 feat: 贪心 · chenshiwei-io/leetcode@0c1786a · GitHub
[go: up one dir, main page]

Skip to content

Commit 0c1786a

Browse files
author
陈世伟
committed
feat: 贪心
1 parent 237bd62 commit 0c1786a

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package _68_监控二叉树
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* type TreeNode struct {
6+
* Val int
7+
* Left *TreeNode
8+
* Right *TreeNode
9+
* }
10+
*/
11+
12+
type TreeNode struct {
13+
Val int
14+
Left *TreeNode
15+
Right *TreeNode
16+
}
17+
18+
func minCameraCover(root *TreeNode) int {
19+
var fn func(root *TreeNode) int
20+
var result int
21+
fn = func(root *TreeNode) int {
22+
if root == nil {
23+
return 2
24+
}
25+
left := fn(root.Left)
26+
right := fn(root.Right)
27+
28+
if left == 2 && right == 2 {
29+
return 0
30+
}
31+
32+
if left == 0 || right == 0 {
33+
result++
34+
return 1
35+
}
36+
37+
if left == 1 || right == 1 {
38+
return 2
39+
}
40+
41+
return -1
42+
}
43+
44+
if fn(root) == 0 {
45+
result++
46+
}
47+
48+
return result
49+
}

0 commit comments

Comments
 (0)
0