当前位置: 首页 > article >正文

力扣labuladong——一刷day62

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

文章目录

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


前言


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

一、力扣1245. 树的直径

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. 监控二叉树

/**
 * 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. 在二叉树中分配硬币

/**
 * 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;
    }
}

http://www.kler.cn/a/153523.html

相关文章:

  • 【CAD二次开发】标注箭头,获取修改标注箭头图块
  • C++11线程以及线程同步
  • (数据结构)顺序表的插入删除
  • Mysql中的引擎介绍(InnoDB,MyISAM,Memory)
  • C# 使用PanGu分词
  • 使用MD5当做文件的唯一标识,这样安全么?
  • Redis 基本命令—— 超详细操作演示!!!
  • 【算法刷题】Day9
  • 使用visual Studio MFC 平台实现对灰度图添加椒盐噪声,并进行均值滤波与中值滤波
  • 快速筛出EXCEL行中的重复项
  • [NOIP2002 普及组] 过河卒
  • 数据结构—二叉树
  • python+pytest接口自动化(2)-HTTP协议基础
  • Selenium+Python自动化测试之验证码处理
  • CentOS 7 配置tomcat
  • 兼容jlink OB arm仿真器使用(杜邦线过长导致烧写总是失败)
  • 五、关闭三台虚拟机的防火墙和Selinux
  • Tomcat 漏洞修复
  • LC.1094. 拼车(差分)
  • 面试篇spark(spark core,spark sql,spark 优化)