代码随想录 Day16 - 二叉树(三)

简介: 代码随想录 Day16 - 二叉树(三)

作业题


104. 二叉树的最大深度

递归找到左右子树的最大深度,取其中的较大值+1,即是答案。

package jjn.carl.binary_tree;
import commons.BinaryTreeHelper;
import commons.TreeNode;
import java.util.ArrayList;
import java.util.List;
/**
 * @author Jiang Jining
 * @since 2023-07-14 23:41
 */
public class LeetCode104 {
    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int maxLeft = maxDepth(root.left);
        int maxRight = maxDepth(root.right);
        return Math.max(maxLeft, maxRight) + 1;
    }
    public static void main(String[] args) {
        List<Integer> input = new ArrayList<>();
        input.addAll(List.of(3, 9, 20));
        input.add(null);
        input.add(null);
        input.addAll(List.of(15, 7));
        TreeNode node = BinaryTreeHelper.buildTree(input);
        int maxDepth = new LeetCode104().maxDepth(node);
        System.out.println("maxDepth = " + maxDepth);
    }
}


111. 二叉树的最小深度

与 104 不一样的是,最小深度要处理仅有左节点或者右节点的情况。

package jjn.carl.binary_tree;
import commons.BinaryTreeHelper;
import commons.TreeNode;
import java.util.ArrayList;
import java.util.List;
/**
 * @author Jiang Jining
 * @since 2023-07-14 23:46
 */
public class LeetCode111 {
    public int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null || root.right == null) {
            return root.left == null ? minDepth(root.right) + 1: minDepth(root.left) + 1;
        }
        int minLeft = minDepth(root.left);
        int minRight = minDepth(root.right);
        return Math.min(minLeft, minRight) + 1;
    }
    public static void main(String[] args) {
        List<Integer> input = new ArrayList<>();
        input.addAll(List.of(3, 9, 20));
        input.add(null);
        input.add(null);
        input.addAll(List.of(15, 7));
        TreeNode node = BinaryTreeHelper.buildTree(input);
        int minDepth = new LeetCode111().minDepth(node);
        System.out.println("minDepth = " + minDepth);
    }
}


222. 完全二叉树的节点个数

package jjn.carl.binary_tree;
import commons.BinaryTreeHelper;
import commons.TreeNode;
import java.util.List;
/**
 * @author Jiang Jining
 * @since 2023-07-15 0:00
 */
public class LeetCode222 {
    public int countNodes(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftCount = countNodes(root.left);
        int rightCount = countNodes(root.right);
        return leftCount + rightCount + 1;
    }
    public static void main(String[] args) {
        List<Integer> list = List.of(1, 2, 3, 4, 5, 6);
        TreeNode node = BinaryTreeHelper.buildTree(list);
        int countNodes = new LeetCode222().countNodes(node);
        System.out.println("countNodes = " + countNodes);
    }
}



目录
相关文章
|
SQL 关系型数据库 MySQL
mysql子查询、聚合函数
mysql子查询、聚合函数
188 0
|
数据可视化 Python
详尽分享用Python进行时间序列预测的7种方法
详尽分享用Python进行时间序列预测的7种方法
117 0
|
编解码 运维 监控
轻松处理高于平常10倍的视频需求,还能节省60%的IT成本,蓝墨做对了什么?
如果说Serverless到底解决了什么问题,核心就是节约成本、节省精力。
3420 61
轻松处理高于平常10倍的视频需求,还能节省60%的IT成本,蓝墨做对了什么?
|
14天前
|
存储 弹性计算 人工智能
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
2025年9月24日,阿里云弹性计算团队多位产品、技术专家及服务器团队技术专家共同在【2025云栖大会】现场带来了《通用计算产品发布与行业实践》的专场论坛,本论坛聚焦弹性计算多款通用算力产品发布。同时,ECS云服务器安全能力、资源售卖模式、计算AI助手等用户体验关键环节也宣布升级,让用云更简单、更智能。海尔三翼鸟云服务负责人刘建锋先生作为特邀嘉宾,莅临现场分享了关于阿里云ECS g9i推动AIoT平台的场景落地实践。
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
|
6天前
|
云安全 人工智能 安全
Dify平台集成阿里云AI安全护栏,构建AI Runtime安全防线
阿里云 AI 安全护栏加入Dify平台,打造可信赖的 AI
|
9天前
|
人工智能 运维 Java
Spring AI Alibaba Admin 开源!以数据为中心的 Agent 开发平台
Spring AI Alibaba Admin 正式发布!一站式实现 Prompt 管理、动态热更新、评测集构建、自动化评估与全链路可观测,助力企业高效构建可信赖的 AI Agent 应用。开源共建,现已上线!
856 31