LeetCode——二叉树的最小深度

LeetCode——二叉树的最小深度

题目描述:
给定一个二叉树,找出其最小深度。

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

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

示例 1:
在这里插入图片描述
输入:root = [3,9,20,null,null,15,7]
输出:2
示例 2:

输入:root = [2,null,3,null,4,null,5,null,6]
输出:5

提示:

树中节点数的范围在 [0, 105] 内
-1000 <= Node.val <= 1000

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/minimum-depth-of-binary-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解法一:
我们用递归的方法解决这个问题。我们要求出整棵树的最小深度,就要求出左右子树的最小深度,并进行比较,整棵树的最小深度就等于min(左子树最小深度,右子树最小深度) + 1。
我们在递归函数中首先判断传入的根节点是否为空,为空就返回0,并判断该节点的左右子树是否都不存在,如果都不存在,即该节点为叶子节点,返回1。我们分别计算左子树和右子树的最小深度,分别存储在leftDepth和rightDepth参数中,然后判断是否有其一为0(也就是左子树或者右子树其一为空),若有,则返回leftDepth + rightDepth + 1 ;否则(也就是左右子树都存在),则返回min(左子树最小深度,右子树最小深度) + 1。

解法二:
我们用广度优先遍历的方法,有关广度优先遍历的可以看我的另一篇文章,题目和这个非常类似。二叉树的最大深度
我们只需要在遍历的过程中判断是否遇到了叶子节点(左子树和右子树为空),如果遇到了就直接返回目前的层数,此时即为最小深度。

这里提供两种解法的Java代码和Python代码。

解法一Java代码:

/**
 * 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;
        if (root.left == null && root.right == null)
            return 1;
        int leftDepth = minDepth(root.left);
        int rightDepth = minDepth(root.right);
        return (leftDepth == 0 || rightDepth == 0)?(leftDepth + rightDepth + 1):(Math.min(leftDepth, rightDepth) + 1);
    }
}

LeetCode测试结果:
在这里插入图片描述

解法一Python代码:

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def minDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if root is None:
            return 0
        if (root.left is None) and (root.right is None):
            return 1
        leftDepth = self.minDepth(root.left)
        rightDepth = self.minDepth(root.right)
        if leftDepth == 0 or rightDepth == 0:
            return leftDepth + rightDepth + 1
        else:
            return min(leftDepth, rightDepth) + 1

LeetCode测试结果:
在这里插入图片描述

解法二Java代码:

/**
 * 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;
        Queue<TreeNode> queue = new LinkedList<>();
        int count = 0;
        queue.offer(root);
        count++;
        while (!queue.isEmpty()){
            int size = queue.size();
            while (size > 0){
                TreeNode node = queue.poll();
                if (node.left == null && node.right == null)
                    return count;
                if (node.left != null)
                    queue.offer(node.left);
                if (node.right != null)
                    queue.offer(node.right);
                size--;
            }
            count++;
        }
        return count;
    }
}

LeetCode测试结果:
在这里插入图片描述

解法二Python代码:

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def minDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if root is None:
            return 0
        TreeList = [root]
        count = 1
        while TreeList:
            size = len(TreeList)
            while size > 0:
                if (TreeList[0].left is None) and (TreeList[0].right is None):
                    return count
                if TreeList[0].left is not None:
                    TreeList.append(TreeList[0].left)
                if TreeList[0].right is not None:
                    TreeList.append(TreeList[0].right)
                del TreeList[0]
                size -= 1
            count += 1
        return count

LeetCode测试结果:
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值