Files
LeetCode-Go/leetcode/0968.Binary-Tree-Cameras/968. Binary Tree Cameras.go
2020-08-07 17:06:53 +08:00

49 lines
855 B
Go

package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
type status int
const (
isLeaf status = iota
parentofLeaf
isMonitoredWithoutCamera
)
func minCameraCover(root *TreeNode) int {
res := 0
if minCameraCoverDFS(root, &res) == isLeaf {
res++
}
return res
}
func minCameraCoverDFS(root *TreeNode, res *int) status {
if root == nil {
return 2
}
left, right := minCameraCoverDFS(root.Left, res), minCameraCoverDFS(root.Right, res)
if left == isLeaf || right == isLeaf {
*res++
return parentofLeaf
} else if left == parentofLeaf || right == parentofLeaf {
return isMonitoredWithoutCamera
} else {
return isLeaf
}
}