968. Binary Tree Cameras

思路:如果子节点只能覆盖到父节点、当前节点,但是父节点可以覆盖到他的父节点、子节点、当前节点,所以从叶子节点往上考虑

0代表子节点没有被覆盖

1代表子节点被覆盖,但是子节点没有camera

2代表子节点被覆盖,子节点有camera

https://www.cnblogs.com/ethanhong/p/10200550.html

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minCameraCover(TreeNode* root) {
int sum = ;
if(minCameraCover(root,sum) == )
sum++;
return sum;
}
int minCameraCover(TreeNode* root,int& sum){
if(!root)
return ;
int left = minCameraCover(root->left,sum);
int right = minCameraCover(root->right,sum);
if(left == || right == ){
sum++;
return ;
}
else if(left == || right == )
return ;
else
return ;
}
};

leetcode 968. Binary Tree Cameras的更多相关文章

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

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

  2. LC 968. Binary Tree Cameras

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

  3. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

  4. LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

  5. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

  6. leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II

    leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  7. [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  8. LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

    翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...

  9. LeetCode—— Invert Binary Tree

    LeetCode-- Invert Binary Tree Question invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 ...

随机推荐

  1. 微信小程序引入外部组件 iView Weapp

    iview Weapp组件的使用方法: 1. 下载小程序组件库 (前提是你已经有了项目目录) 你可以直接去github把iView Weapp的代码下载下来,也可以用过npm来下载. github地址 ...

  2. Linux基础学习之基础命令(1)--2019-11-14

    查看命令路径其他方法: which 命令: which [options] [--] programname [...] -a:显示所有匹配的程序文件,而非第一个: --skip-alias:略过别名 ...

  3. docker命令小全 this is my note.

    服务器类型:linux =>centos 7.X以上版本 常用命令使用紫色加粗标明 1.安装yum-util(为配置docker安装时使用阿里镜像做准备):yum install -y yum- ...

  4. 一键配置树莓派wifi、静态ip等

    系统老是被我玩坏,重新烧录系统后配置又太麻烦,用shell脚本一键配置一下wifi和静态IP #!/bin/bash touch /media/fanghaoyu/boot/ssh echo &quo ...

  5. qt5.12 解决显示中文乱码问题

    在菜单栏   文件->选项,找到文本编辑器 文件编码设置如图 在cpp文件中加入 #pragma execution_character_set("utf-8") 之后就可以 ...

  6. 算法 dfs —— 将二叉树 先序遍历 转为 链表

    将二叉树拆成链表 中文English 将一棵二叉树按照前序遍历拆解成为一个 假链表.所谓的假链表是说,用二叉树的 right 指针,来表示链表中的 next 指针. Example 样例 1: 输入: ...

  7. 【转】JAVA接口自动化测试之一个测试方法对应多条测试数据的实现方式

    一.痛点:一条测试数据对应一个测试方法 前面的章节中我们已经写代码实现了登录接口的处理调用,但是一个接口往往是需要多条测试用例才能完整的覆盖到每一种情况. 针对于单接口多条测试用例需要执行的情况,该如 ...

  8. Vuex之辅助函数

    mapState.mapGetters.mapActions 如果我们不喜欢这种在页面上使用“this.$stroe.state.count”和“this.$store.dispatch('funNa ...

  9. javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.qingmu.Customer

    javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity pas ...

  10. 20180606模拟赛T1——猫鼠游戏

    题目描述: 猫和老鼠在10*10的方格中运动,例如: *...*..... ......*... ...*...*.. .......... ...*.C.... *.....*... ...*... ...