代码随想录算法训练营DAY16 | 二叉树 (3)

本文介绍了如何使用递归方法解决LeetCode中的四个二叉树相关问题:最大深度、最小深度以及计算完全二叉树的节点个数。通过后序遍历实现,展示了在不同情况下的逻辑处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、LeetCode 104 二叉树的最大深度

题目链接:104.二叉树的最大深度icon-default.png?t=N7T8https://leetcode.cn/problems/maximum-depth-of-binary-tree/

思路:采用后序遍历递归求解。

class Solution {
    int ans = 0;
    public int maxDepth(TreeNode root) {
        if(root == null){
            return 0;
        }
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        int depth = 1 + Math.max(left,right);
        return depth;
    }
}
/**
 * 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;
 *     }
 * }
 */

二、LeetCode 111 二叉树的最小深度

题目链接:111.二叉树的最小深度icon-default.png?t=N7T8https://leetcode.cn/problems/minimum-depth-of-binary-tree/

思路:左右孩子均为空才是叶子节点,才可以计算深度。

class Solution {
    public int minDepth(TreeNode root) {
        if(root == null){
            return 0;
        }
        //左
        int left = minDepth(root.left);
        //右
        int right = minDepth(root.right);
        //中
        //左子树为空、右子树不空的情况(非叶子节点)
        if(root.left == null && root.right != null){
            return 1 + right;
        }
        //右子树为空,左子树不空的情况
        if(root.left != null && root.right == null){
            return 1 + left;
        }
        return 1 + Math.min(left,right);
    }
}
/**
 * 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;
 *     }
 * }
 */

三、LeetCode 222 完全二叉树的节点个数

题目链接:222.完全二叉树的节点个数icon-default.png?t=N7T8https://leetcode.cn/problems/count-complete-tree-nodes/description/

思路:分别计算左右子树节点个数,再相加。

class Solution {
    public int countNodes(TreeNode root) {
        if(root == null){
            return 0;
        }
        //分别计算左右子树节点个数 再相加
        int left = countNodes(root.left);
        int right = countNodes(root.right);
        return left + right + 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;
 *     }
 * }
 */

四、今日小结

        偷得浮生半日闲~

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

橙南花已开

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

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

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

打赏作者

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

抵扣说明:

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

余额充值