From c8ddb46c9d4a000fbfb1498c0be32af758b911e5 Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Tue, 19 Oct 2021 15:18:20 +0800 Subject: [PATCH] =?UTF-8?q?968:=E7=9B=91=E6=8E=A7=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetcode/editor/cn/BinaryTreeCameras.java | 86 +++++++++++++++++++ .../cn/doc/content/BinaryTreeCameras.md | 34 ++++++++ 2 files changed, 120 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/BinaryTreeCameras.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/BinaryTreeCameras.md diff --git a/src/main/java/leetcode/editor/cn/BinaryTreeCameras.java b/src/main/java/leetcode/editor/cn/BinaryTreeCameras.java new file mode 100644 index 0000000..3d62486 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/BinaryTreeCameras.java @@ -0,0 +1,86 @@ +//给定一个二叉树,我们在树的节点上安装摄像头。 +// +// 节点上的每个摄影头都可以监视其父对象、自身及其直接子对象。 +// +// 计算监控树的所有节点所需的最小摄像头数量。 +// +// +// +// 示例 1: +// +// +// +// 输入:[0,0,null,0,0] +//输出:1 +//解释:如图所示,一台摄像头足以监控所有节点。 +// +// +// 示例 2: +// +// +// +// 输入:[0,0,null,0,null,0,null,null,0] +//输出:2 +//解释:需要至少两个摄像头来监视树的所有节点。 上图显示了摄像头放置的有效位置之一。 +// +// +// +//提示: +// +// +// 给定树的节点数的范围是 [1, 1000]。 +// 每个节点的值都是 0。 +// +// Related Topics 树 深度优先搜索 动态规划 二叉树 👍 330 👎 0 + +package leetcode.editor.cn; + +import com.code.leet.entiy.TreeNode; + +//968:监控二叉树 +class BinaryTreeCameras { + public static void main(String[] args) { + //测试代码 + Solution solution = new BinaryTreeCameras().new Solution(); + } + + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) + + /** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ + class Solution { + public int minCameraCover(TreeNode root) { + int[] array = dfs(root); + return array[1]; + } + + public int[] dfs(TreeNode root) { + if (root == null) { + return new int[]{Integer.MAX_VALUE / 2, 0, 0}; + } + int[] leftArray = dfs(root.left); + int[] rightArray = dfs(root.right); + int[] array = new int[3]; + array[0] = leftArray[2] + rightArray[2] + 1; + array[1] = Math.min(array[0], Math.min(leftArray[0] + rightArray[1], rightArray[0] + leftArray[1])); + array[2] = Math.min(array[0], leftArray[1] + rightArray[1]); + return array; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/doc/content/BinaryTreeCameras.md b/src/main/java/leetcode/editor/cn/doc/content/BinaryTreeCameras.md new file mode 100644 index 0000000..a72e7df --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/BinaryTreeCameras.md @@ -0,0 +1,34 @@ +

给定一个二叉树,我们在树的节点上安装摄像头。

+ +

节点上的每个摄影头都可以监视其父对象、自身及其直接子对象。

+ +

计算监控树的所有节点所需的最小摄像头数量。

+ +

 

+ +

示例 1:

+ +

+ +
输入:[0,0,null,0,0]
+输出:1
+解释:如图所示,一台摄像头足以监控所有节点。
+
+ +

示例 2:

+ +

+ +
输入:[0,0,null,0,null,0,null,null,0]
+输出:2
+解释:需要至少两个摄像头来监视树的所有节点。 上图显示了摄像头放置的有效位置之一。
+
+ +


+提示:

+ +
    +
  1. 给定树的节点数的范围是 [1, 1000]
  2. +
  3. 每个节点的值都是 0。
  4. +
+
Related Topics
  • 深度优先搜索
  • 动态规划
  • 二叉树

  • 👍 330
  • 👎 0
  • \ No newline at end of file