算法训练营 day16 二叉树 二叉树的最大深度 二叉树的最小深度 完全二叉树的节点个数

算法训练营 day16 二叉树 二叉树的最大深度 二叉树的最小深度 完全二叉树的节点个数

二叉树的最大深度

104. 二叉树的最大深度 - 力扣(LeetCode)

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

递归法

本题可以使用前序(中左右),也可以使用后序遍历(左右中),使用前序求的就是深度,使用后序求的是高度。

  • 二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
  • 二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数后者节点数(取决于高度从0开始还是从1开始)

而根节点的高度就是二叉树的最大深度,所以本题中我们通过后序求的根节点高度来求的二叉树最大深度。

class Solution {
    public int maxDepth(TreeNode root) {
        if(root==null) return 0;
        int leftheight = maxDepth(root.left);
        int rightheight = maxDepth(root.right);
        int height = 1+Math.max(leftheight,rightheight);
        return height;
    }
}

层序遍历

使用迭代法的话,使用层序遍历是最为合适的,因为最大的深度就是二叉树的层数,和层序遍历的方式极其吻合。

在这里插入图片描述

class Solution {
    public int maxDepth(TreeNode root) {
        if(root==null) return 0;
        Queue<TreeNode> que = new LinkedList<>();
        que.add(root);
        int depth = 0;
        TreeNode node;
        while (!que.isEmpty()) {
            int size = que.size();
            depth++;
            while (size-->0){
                node = que.poll();
                if (node.left!=null)que.add(node.left);
                if (node.right!=null)que.add(node.right);
            }
        }
        return depth;
    }
}

再来一题

559. N 叉树的最大深度 - 力扣(LeetCode)

给定一个 N 叉树,找到其最大深度。

最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。

N 叉树输入按层序遍历序列化表示,每组子节点由空值分隔(请参见示例)。

思路大致一样

递归法
class Solution {
    public int maxDepth(Node root) {
        if (root == null) return 0;
        int depth = 0;
        for (Node child : root.children){
                depth = Math.max(depth, maxDepth(child));
        }
        return depth + 1; //中节点
    }  
}
层序遍历
class Solution {
    public int maxDepth(Node root) {
        if(root==null) return 0;
        Queue<Node> que = new LinkedList<>();
        que.add(root);
        int depth = 0;
        Node node;
        while (!que.isEmpty()) {
            int size = que.size();
            depth++;
            while (size-->0){
                node = que.poll();
                for (Node ch: node.children) {
                    if (ch!=null)que.add(ch);
                }
            }
        }
        return depth;
    }
}

二叉树的最小深度

111. 二叉树的最小深度 - 力扣(LeetCode)

给定一个二叉树,找出其最小深度。

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

在这里插入图片描述

递归法

class Solution {
    public int minDepth(TreeNode root) {
        if (root == null) return 0;
        int leftheight = minDepth(root.left);
        int rightheight = minDepth(root.right);
        if (root.left!=null&&root.right==null) return 1+leftheight;
        if (root.left==null&&root.right!=null) return 1+rightheight;
        int height = 1 + Math.min(leftheight, rightheight);
        return height;
    }
}

层序遍历

class Solution {
    public int minDepth(TreeNode root) {
        if (root == null) return 0;
        int depth =0;
        Queue<TreeNode> que = new LinkedList<>();
        TreeNode node;
        que.add(root);
        while (!que.isEmpty()){
            int size = que.size();
            depth++;
            while (size-->0){
                node = que.poll();
                if (node.left==null&&node.right==null){
                    return depth;
                }
                if (node.left!=null)que.add(node.left);
                if (node.right!=null)que.add(node.right);
            }
        }
        return depth;
    }
}

完全二叉树的节点个数

222. 完全二叉树的节点个数 - 力扣(LeetCode)

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

完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。

按照普通二叉树即可求解

递归法

class Solution {
    public int countNodes(TreeNode root) {
        if (root == null) return 0;
        return countNodes(root.left)+countNodes(root.right)+1;
    }
}

层序遍历

class Solution {
    public int countNodes(TreeNode root) {
        if (root == null) return 0;
        int nums =0;
        Queue<TreeNode> que = new LinkedList<>();
        TreeNode node;
        que.add(root);
        while (!que.isEmpty()){
            int size = que.size();
            nums +=size;
            while (size-->0){
                node = que.poll();
                if (node.left!=null)que.add(node.left);
                if (node.right!=null)que.add(node.right);
            }
        }
        return nums;
    }
}

使用完全二叉树

class Solution {
    //满二叉树的结点数为:2^depth - 1
    public int countNodes(TreeNode root) {
        if (root == null) return 0;
        TreeNode left = root.left;
        TreeNode right = root.right;
        int leftDepth=0,rightDepth=0;// 这里初始为0是有目的的,为了下面求指数方便
        while (left!=null){
            left = left.left;
            leftDepth++;
        }
        while (right!=null){
            right = right.right;
            rightDepth++;
        }
        if (leftDepth==rightDepth){
            return (2<<leftDepth)-1;// 注意(2<<1) 相当于2^2,所以leftDepth初始为0
        }
        return countNodes(root.left)+countNodes(root.right)+1;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

还是选择了面包

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值