关注Java领域相关技术 记录有趣的事情

LeetCode-111. 二叉树的最小深度

US-B.Ralph
US-B.Ralph
2020-08-21

问题地址

LeetCode每日一题/2020-08-21

LeetCode111. 二叉树的最小深度


问题描述

规则

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

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

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

示例1

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

    3
   / \
  9  20
    /  \
   15   7
返回它的最小深度  2.
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

解析

解题思路

根据题目我们需要找出树的最小深度,要找树的最小深度我们只需要找出左右子树的最小深度即可,所以使用递归的方法解决这个问题:

  • 如果root为空,返回0;
  • 如果左右子节点均为空,返回1;
  • 否则返回左右子树的最小深度+1(root):

复杂度分析

  1. 时间复杂度,O(N),其中 N 是树的节点数。对每个节点访问一次;

  2. 空间复杂度,O(H),其中 H 是树的高度。空间复杂度主要取决于递归时栈空间的开销,最坏情况下,树呈现链状,空间复杂度为 O(N)。平均情况下树的高度与节点数的对数正相关,空间复杂度为 O(logN)

定位问题

  1. 计算子树的深度;

数据操作分析

  1. 遍历树结构,对于非叶子节点分别计算左右子树深度;

编码实现

/**
 * LeetCode529
 */
public class MinDepthOfBinaryTree {
    public int minDepth(TreeNode root) {
        if (root == null) return 0;
        if (root.right == null && root.left == null) return 1;

        int treeDepth = 1 + minDepth(root.left, root.right);

        return treeDepth;

    }

    private int minDepth(TreeNode left, TreeNode right) {
        if (left == null) return minDepth(right);
        if (right == null) return minDepth(left);

        return Math.min(minDepth(left), minDepth(right));
    }
}

官方解法

  1. 深度优先搜索
  • 思路:
    • 首先可以想到使用深度优先搜索的方法,遍历整棵树,记录最小深度。

    • 对于每一个非叶子节点,我们只需要分别计算其左右子树的最小叶子节点深度。这样就将一个大问题转化为了小问题,可以递归地解决该问题。

  • 复杂度分析:

    • 时间复杂度:O(N),其中 N 是树的节点数。对每个节点访问一次。

    • 空间复杂度:O(H),其中 H 是树的高度。空间复杂度主要取决于递归时栈空间的开销,最坏情况下,树呈现链状,空间复杂度为 O(N)。平均情况下树的高度与节点数的对数正相关,空间复杂度为 O(logN)

class Solution {
    public int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }

        if (root.left == null && root.right == null) {
            return 1;
        }

        int min_depth = Integer.MAX_VALUE;
        if (root.left != null) {
            min_depth = Math.min(minDepth(root.left), min_depth);
        }
        if (root.right != null) {
            min_depth = Math.min(minDepth(root.right), min_depth);
        }

        return min_depth + 1;
    }
}
  1. 广度优先搜索
  • 思路:

    • 同样,我们可以想到使用广度优先搜索的方法,遍历整棵树。
    • 当我们找到一个叶子节点时,直接返回这个叶子节点的深度。广度优先搜索的性质保证了最先搜索到的叶子节点的深度一定最小。
  • 复杂度分析:
    • 时间复杂度:O(N),其中 N 是树的节点数。对每个节点访问一次。

    • 空间复杂度:O(N),其中 N 是树的节点数。空间复杂度主要取决于队列的开销,队列中的元素个数不会超过树的节点数。

class Solution {
    class QueueNode {
        TreeNode node;
        int depth;

        public QueueNode(TreeNode node, int depth) {
            this.node = node;
            this.depth = depth;
        }
    }

    public int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }

        Queue<QueueNode> queue = new LinkedList<QueueNode>();
        queue.offer(new QueueNode(root, 1));
        while (!queue.isEmpty()) {
            QueueNode nodeDepth = queue.poll();
            TreeNode node = nodeDepth.node;
            int depth = nodeDepth.depth;
            if (node.left == null && node.right == null) {
                return depth;
            }
            if (node.left != null) {
                queue.offer(new QueueNode(node.left, depth + 1));
            }
            if (node.right != null) {
                queue.offer(new QueueNode(node.right, depth + 1));
            }
        }

        return 0;
    }
}
US-B.Ralph
LeetCode数据结构与算法算法

Leave a Comment

邮箱地址不会被公开。 必填项已用*标注

12 + 11 =