day16【二叉树】104.二叉树的最大深度 | 559.n叉树的最大深度 | 111.二叉树的最小深度 | 222.完全二叉树的节点个数

104.二叉树的最大深度

  • 104.二叉树的最大深度 | 题目链接
  • 代码随想录 | 讲解链接
  • 题意:给定一个二叉树,找出其最大深度。
    二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
    在这里插入图片描述
  • 思路:根节点的高度就是二叉树的最大深度,通过后序求的根节点高度来求的二叉树最大深度
    1. 确定递归函数的参数和返回值:参数就是传入根节点,返回就返回这棵树的深度,返回值类型为int。
    2. 确定终止条件:如果为空节点的话,就返回0,表示高度为0;
    3. 确定单层递归的逻辑:先求左子树深度,再求右子树深度,最后取左右深度最大的数值,再+1(加1是因为算上当前中间的节点),就是目前节点为根节点的树的深度。
/**
 * 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 {
    public int maxDepth(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int leftDepth = maxDepth(root.left);
        int rightDepth = maxDepth(root.right);
        return Math.max(leftDepth, rightDepth) + 1;
    }
}

559.n叉树的最大深度

/*
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
    public int maxDepth(Node root) {
        if(root == null) {
            return 0;
        }

        int depth = 0;
        if(root.children != null) {
            for(Node child : root.children) {
                depth = Math.max(depth, maxDepth(child));
            }
        }
        return 1 + depth;
    }
}

111.二叉树的最小深度

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

  • 代码随想录 | 讲解链接

  • 题意:最小深度是从根节点到最近的叶子节点的最短路径上的节点数量,叶子节点是左右孩子都为空的节点!
    在这里插入图片描述

  • 思路:递归三部曲

    1. 确定传入参数和返回值:传入根节点,返回最小的深度,类型为int
    2. 确定终止条件:如果遇到节点为null,就返回0,表示当前节点的高度为0;
    3. 确定单层递归逻辑
      • 左子树为null,右子树不为空,最小深度为1+右子树深度
      • 右子树为null,左子树不为空,最小深度是1+左子树深度
      • 如果左右子树都不为空,返回1+左右子树深度的最小值
/**
 * 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 {
    public int minDepth(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int leftDepth = minDepth(root.left);
        int rightDepth = minDepth(root.right);

        if(root.left == null && root.right != null) {
            return 1 + rightDepth;
        } else if(root.left != null && root.right == null) {
            return 1 + leftDepth;
        } 
        
        int res = 1 + Math.min(leftDepth, rightDepth);
        return res;
    }
}

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

  • 222. 完全二叉树的节点个数 | 题目链接
  • 代码随想录 | 讲解链接
  • 题意:给出一个完全二叉树,求出该树的节点个数。
    在这里插入图片描述
  • 思路:递归。先把全二叉树的情况剪枝。
    1. 剪枝全二叉树:判断左右子树的深度是否相同,相同则为全二叉树,不相同则为完全二叉树,就走递归逻辑
    2. 判断终止条件,当节点为null是返回个数0;
    3. 递归逻辑:判断左子树的个数,右子树的个数,最后在加1,加的是中间节点自己本身的个数。
/**
 * 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 {
    public int countNodes(TreeNode root) {
        if(root == null) {
            return 0;
        }
        //判断是否为完全二叉树,如果是全二叉树,就用公式解决。
        TreeNode left = root.left;
        TreeNode right = root.right;
        int leftDepth = 0;
        int rightDepth = 0;
        while(left != null) {
            left = left.left;
            leftDepth++;
        }
        while(right != null) {
            right = right.right;
            rightDepth++;
        }
        if(leftDepth == rightDepth) {
            return (2 << leftDepth) - 1;
        }
        //如果不是全二叉树,是完全二叉树,再走递归逻辑
        return countNodes(root.left) + countNodes(root.right) + 1;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

xuwuuu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值