Leetcode111.二叉树的最小深度

这篇博客介绍了如何找到给定二叉树的最小深度,提供了三种解题思路:递归、深度优先搜索和广度优先搜索。通过示例解释了每个思路的逻辑,并给出了相关的时间和空间复杂度分析。
摘要由CSDN通过智能技术生成

题目

给定一个二叉树,找出其最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

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

示例:

给定二叉树 [3,9,20,null,null,15,7],
3
/ \
9 20
    / \
    15 7

返回它的最小深度 2.

思路一——递归

本题有一个关键点是题目是指根节点到叶子结点的最小深度,当一个结点仅有一个左子树或右子树时,最小深度应该是左子树或右子树深度+1。针对一个结点,存在以下几种情况:

  • 空结点,返回0;
  • 左子树/右子树都无,返回1;
  • 左子树存在,右子树不存在,则minDepth(root.left) + 1;
  • 左子树不存在,右子树存在,则minDepth(root.right)+1;
  • 左右子树都存在,则比较左右子树的最小深度,并取最小值,再+1.
package Algorithms.BinaryTree;

public class Mini_depth_of_binarytree {
    public static class TreeNode{
        //定义二叉树
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x;}
    }

    //递归函数
    public static int minDepth(TreeNode root){
        if(root == null) return 0;
        if(root.left == null && root.right != null) return minDepth(root.right)+1;
        else if(root.left != null && root.right == null) return minDepth(root.left) + 1;
        else return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
    }

    //主函数
    public static void main(String[] args){
        TreeNode root = new TreeNode(1);
        root = Init(root);
        int min = minDepth(root);
        System.out.println(min);

    }
    //初始化二叉树
    public static TreeNode Init(TreeNode root){
        TreeNode pNode = root;
        pNode.left = new TreeNode(2);
        return  root;
    }
}

执行结果1

思路二——深度优先搜索

时间复杂度:O(N);
空间复杂度:最好情况O(logn),最坏情况O(N)

class Solution {
    public int minDepth(TreeNode root) {
        //1、判断特殊情况
        if(root == null) return 0;
        if(root.left == null && root.right == null) return 1;
        int min = Integer.MAX_VALUE;
        if(root.left != null){
            min = Math.min(minDepth(root.left), min);
        }
        if(root.right != null){
            min = Math.min(minDepth(root.right), min);
        }
        return min + 1;
    }
}

代码简化:

class Solution {
    public int minDepth(TreeNode root) {
        //1、判断特殊情况
        if(root == null) return 0;

        int min1 = minDepth(root.left);
        int min2 = minDepth(root.right);

        if(min1 == 0 || min2 == 0) return min1 + min2 + 1;
       
        return Math.min(min1, min2) + 1;
    }
}

执行结果2

思路三——广度优先搜索

层级遍历,遇到第一个叶子结点就返回该结点的深度,即为二叉树的最小深度。

class Solution {
    public int minDepth(TreeNode root) {
        //1、判断特殊情况
        if(root == null) return 0;
        LinkedList<Pair<TreeNode, Integer>> queue = new LinkedList<>();
        queue.add(new Pair(root,1));
        int min_depth = 0;
        while(!queue.isEmpty()){
            Pair<TreeNode, Integer> current = queue.poll();
            root = current.getKey();
            min_depth = current.getValue();
            if(root.right == null && root.left == null) break;
            if(root.left != null) queue.add(new Pair(root.left, min_depth + 1));
            if(root.right != null) queue.add(new Pair(root.right, min_depth + 1));
        }

        return min_depth;
    }
}

执行结果3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值