代码随想录-Day16 | LeetCode104. 二叉树的最大深度、LeetCode111. 二叉树的最小深度、LeetCode222.完全二叉树的节点个数

文档讲解: 代码随想录
视频讲解: 《代码随想录》算法公开课-跟着Carl学算法

LeetCode104. 二叉树的最大深度

题目链接:104. 二叉树的最大深度

题目描述:二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。

思路:第一想法就是利用之前层序遍历的解法,计算二叉树的层数就是最大深度。递归法后续再弄。

class Solution {
    public int maxDepth(TreeNode root) {
        // 直接想到的是利用之前层序遍历的解法 计算二叉树的层数就是最大深度
        // 利用队列先进先出的特性 符合一层一层遍历的逻辑
        if (root == null) {
            return 0;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root); // 根节点加入
        // 设置遍历统计二叉树的最大深度
        int maxDepth = 0;

        while (!queue.isEmpty()) {
            int size = queue.size();
            while (size-- > 0) {
                TreeNode node = queue.poll();
                // 加入根节点的左右节点
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
            }
            maxDepth++;
        }
        return maxDepth;
    }
}

LeetCode111. 二叉树的最小深度

题目链接:111. 二叉树的最小深度

题目描述:给定一个二叉树,找出其最小深度(从根节点到最近叶子节点的最短路径上的节点数量)。

思路:还是利用层序遍历,循环里面多加一个条件,当碰到某个节点没有左右孩子,直接提前终止程序。

class Solution {
    public int minDepth(TreeNode root) {
        // 也可以用层序遍历来解决把
        // 一旦再遍历过程中遇到某个节点都没有左右孩子直接输出
        if (root == null) {
            return 0;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root); // 根节点加入
        // 设置变量统计二叉树的最小深度
        int minDepth = 0;

        while (!queue.isEmpty()) {
            int size = queue.size();
            while (size-- > 0) {
                TreeNode node = queue.poll();
                // 加入根节点的左右节点
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
                if (node.left == null && node.right == null) {
                    return minDepth + 1;
                }
            }
            minDepth++;
        }
        return minDepth;
    }
}

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

题目链接:222.完全二叉树的节点个数

题目描述:给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。

思路:同样利用层序遍历就可以求解,在返回每层的size中更新统计节点个数变量。

class Solution {
    public int countNodes(TreeNode root) {
        // 还是想用层序遍历解法 循环每层时会返回当前size
        if (root == null) {
            return 0;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root); // 根节点加入
        // 设置变量统计完全二叉树的节点个数
        int nodeNum = 0;

        while (!queue.isEmpty()) {
            int size = queue.size();
            nodeNum += size; // 每层节点个数
            while (size-- > 0) {
                TreeNode node = queue.poll();
                // 加入根节点的左右节点
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
            }
        }
        return nodeNum;
    }
}
  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值