力扣labuladong——一刷day62

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • [一、力扣1245. 树的直径](#一、力扣1245. 树的直径)
  • [二、力扣968. 监控二叉树](#二、力扣968. 监控二叉树)
  • [三、力扣979. 在二叉树中分配硬币](#三、力扣979. 在二叉树中分配硬币)

前言


说过后序位置的特殊之处,后序位置可以接收到子树的信息,同时也可以通过函数参数接收到父节点传递的信息,这道题就可以比较完美地体现这一特点

一、力扣1245. 树的直径

java 复制代码
class Solution {
    int max = 0;
    Map<Integer,List<Integer>> map = new HashMap<>();
    HashSet<Integer> set = new HashSet<>();
    public int treeDiameter(int[][] edges) {
        if(edges.length == 0){
            return 0;
        }
        for(int[] e : edges){
            int a = e[0], b = e[1];
            if(!map.containsKey(a)){
                map.put(a,new ArrayList<>());
            }
            if(!map.containsKey(b)){
                map.put(b, new ArrayList<>());
            }
            map.get(a).add(b);
            map.get(b).add(a);
        }
        fun(edges[0][0]);
        return max;
    }
    public int fun(int root){
        if(set.contains(root)){
            return 0;
        }
        set.add(root);
        int first = 0, second = 0;
        for(int a : map.get(root)){
            int child = fun(a);
            if(child >= first){
                second = first;
                first = child;
            }else if(child > second){
                second = child;
            }
        }
        int cur = first + second;
        max = Math.max(max, cur);
        return first + 1;
    }
}

二、力扣968. 监控二叉树

java 复制代码
/**
 * 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 {
    int res = 0;
    public int minCameraCover(TreeNode root) {
        if(fun(root) == 0){
            res ++;
        }
        return res;
    }
    //0表示未覆盖
    //1表示安装摄像头
    //2表示已覆盖
    public int fun(TreeNode root){
        if(root == null){
            return 2;
        }
        int left = fun(root.left);
        int right = fun(root.right);
        if(left == 0 || right == 0){
            res ++;
            return 1;
        }
        if(left == 1 || right == 1){
            return 2;
        }
        if(left == 2 && right == 2){
            return 0;
        }
        return 0;
    }
}

三、力扣979. 在二叉树中分配硬币

java 复制代码
/**
 * 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 {
    int res = 0;
    public int distributeCoins(TreeNode root) {
        fun(root);
        return res;
    }
    public int fun(TreeNode root){
        if(root == null){
            return 0;
        }
        int left = fun(root.left);
        int right = fun(root.right);
        res += Math.abs(left) + Math.abs(right) + root.val - 1;
        return left + right + root.val - 1;
    }
}
相关推荐
独自破碎E1 天前
Java对象是怎么在虚拟机中存储的?
java·开发语言
兮动人1 天前
打破 OS 壁垒:Java 跨平台硬件信息采集的“终极方案”
java·开发语言
json{shen:"jing"}1 天前
07_表单输入绑定
java·前端·javascript
Xの哲學1 天前
Linux SLUB 内存分配器深度剖析: 从设计哲学到实战调试
linux·服务器·网络·算法·边缘计算
tobias.b1 天前
408真题解析-2009-1-数据结构-队列-进出规则-应用
数据结构·考研·408真题
2401_876221341 天前
AtCoder Beginner Contest 439 - D - Kadomatsu Subsequence
c++·算法
小丁努力不焦虑1 天前
算法期末总结题
数据结构·算法
嵌入式进阶行者1 天前
【算法】从数组中选取两个符合一定条件的数的算法与实例:华为OD机考双机位A卷 - 跳房子I
数据结构·c++·算法·链表
老歌老听老掉牙1 天前
从战场到商场:最优化算法如何用数学重塑世界?
python·算法·最优化
zhaokuner1 天前
12-深层模型与重构-DDD领域驱动设计
java·开发语言·设计模式·架构