2019-03-27 15:39:37

问题描述:

问题求解:

很有意思的问题,问题描述简单,求解过程也可以非常的简洁,是个难得的好题。

求解的过程是自底向上进行分析,对于叶子节点,如果在叶子上放置照相机,显然是没有在其parent上放置相机来的合适的,因为叶子节点的覆盖范围没有其parent节点大。

因此,我们就可以对所有的叶子的节点的parent进行放置相机,同时将所有已经覆盖掉的节点去除掉。

对于剩下的节点重复上述的操作即可。

这里在求解的时候并不需要真实的去删除节点,只要在每个节点上加上标注信息就可以了。

叶子节点 :1

放置照相机 :2

已经被cover节点 :3

    public int minCameraCover(TreeNode root) {
int[] res = new int[1];
int state = helper(root, res);
if (state == 1) res[0]++;
return res[0];
} // 1 : leaf
// 2 : camera
// 3 : covered
private int helper(TreeNode root, int[] res) {
if (root == null) return 3;
int l = helper(root.left, res);
int r = helper(root.right, res);
if (l == 3 && r == 3) return 1;
else if (l == 1 || r == 1) {
res[0]++;
return 2;
}
else return 3;
}

  

二叉树放置照相机 Binary Tree Cameras的更多相关文章

  1. 笔试算法题(41):线索二叉树(Threaded Binary Tree)

    议题:线索二叉树(Threaded Binary Tree) 分析: 为除第一个节点外的每个节点添加一个指向其前驱节点的指针,为除最后一个节点外的每个节点添加一个指向其后续节点的指针,通过这些额外的指 ...

  2. leetcode 968. Binary Tree Cameras

    968. Binary Tree Cameras 思路:如果子节点只能覆盖到父节点.当前节点,但是父节点可以覆盖到他的父节点.子节点.当前节点,所以从叶子节点往上考虑 0代表子节点没有被覆盖 1代表子 ...

  3. Leetcode之分治法专题-654. 最大二叉树(Maximum Binary Tree)

    Leetcode之分治法专题-654. 最大二叉树(Maximum Binary Tree) 给定一个不含重复元素的整数数组.一个以此数组构建的最大二叉树定义如下: 二叉树的根是数组中的最大元素. 左 ...

  4. [Swift]LeetCode968.监控二叉树 | Binary Tree Cameras

    Given a binary tree, we install cameras on the nodes of the tree. Each camera at a node can monitor  ...

  5. 【LeetCode】968. Binary Tree Cameras 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. [Swift]LeetCode563. 二叉树的坡度 | Binary Tree Tilt

    Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the ab ...

  7. (二叉树 BFS) leetcode102. Binary Tree Level Order Traversal

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  8. 剑指offer从上往下打印二叉树 、leetcode102. Binary Tree Level Order Traversal(即剑指把二叉树打印成多行、层序打印)、107. Binary Tree Level Order Traversal II 、103. Binary Tree Zigzag Level Order Traversal(剑指之字型打印)

    从上往下打印二叉树这个是不分行的,用一个队列就可以实现 class Solution { public: vector<int> PrintFromTopToBottom(TreeNode ...

  9. 【构建二叉树】02根据中序和后序序列构造二叉树【Construct Binary Tree from Inorder and Postorder Traversal】

    我们都知道,已知中序和后序的序列是可以唯一确定一个二叉树的. 初始化时候二叉树为:================== 中序遍历序列,           ======O=========== 后序遍 ...

随机推荐

  1. C# 封装winio.dll 驱动级按键鼠标操作模拟

    using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices ...

  2. Python网络编程-Socket简单通信(及python实现远程文件发送)

    学习python中使用python进行网络编程,编写简单的客户端和服务器端进行通信,大部分内容来源于网络教程,这里进行总结供以后查阅. 先介绍下TCP的三次握手: 1,简单的发送消息: 服务器端: i ...

  3. Ubuntu系统实现将Jupyter notebook项目发布到GitHub

    一.准备 Ubuntu16.04.GitHub账户.Git.Jupyter Notebook项目 二.打开Terminal(用户和邮箱即为你的GitHub注册的账号和邮箱) git config -- ...

  4. python练习题-day9

    2.写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者. def dan_index(itter): li=[] for i in range(len(itte ...

  5. 266B

    #include <stdio.h> #define MAXSIZE 1024 char que[MAXSIZE]; int main() { int n, t; scanf(" ...

  6. 【LeetCode每天一题】Jump Game(跳跃游戏)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  7. Django进阶之中间件

    中间件简介 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. 在djang ...

  8. ucli tcl cmd

    ucli接口与tcl 8.6兼容:vcs中要调用ucli接口,执行脚本,必须在compile的时候,加入debug的权限: -debug,-debug_pp,-debug_all,-debug_acc ...

  9. 关于easy ui 的combobox遍历选中

    $.post("fleetAction!queryAllCompanyByCompanyID.do",{"truckCompany.id":companyId} ...

  10. Jmeter对jar包的调用赋值

    一.前言 在我们测试接口的过程中,可能有时需要用到第三方jar包来生成一些测试数据(如有时需要对参数的输入值使用第三方jar包进行加密操作),涉及到这种的情况,普遍做法是:手动调用jar包获得需要的值 ...