前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >leetcode刷题(50)——111. 二叉树的最小深度

leetcode刷题(50)——111. 二叉树的最小深度

作者头像
老马的编程之旅
发布2022-06-22 13:40:28
1520
发布2022-06-22 13:40:28
举报
文章被收录于专栏:深入理解Android

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

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

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

示例:

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

代码语言:javascript
复制
 3
   / \
  9  20
    /  \
   15   7

返回它的最小深度 2.

方法1:递归 对比求最大深度,只有一个地方需要注意,那就是如果左右子树有一边为null而另一边不为null,最小深度不是0+1,而是另一个不为null子树的最小深度+1

代码语言:javascript
复制
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int minDepth(TreeNode root) {
        return findDepth(root);
    }

     public int findDepth(TreeNode root){
         if(root==null){
            return 0;
        }
        if(root.left==null&&root.right==null){
            return 1;
        }
        int leftDepth = findDepth(root.left);
        int rightDepth = findDepth(root.right);
        if(leftDepth==0&&rightDepth!=0){
            return rightDepth+1;
        }
        if(rightDepth==0&&leftDepth!=0){
            return leftDepth+1;
        }
        return Math.min(leftDepth,rightDepth)+1;
    }
}

上面还可以整理成更整洁的写法:

代码语言:javascript
复制
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int minDepth(TreeNode root) {
        return findDepth(root);
    }

     public int findDepth(TreeNode root){
         if(root==null){
            return 0;
        }
        int leftDepth = findDepth(root.left);
        int rightDepth = findDepth(root.right);
        
        return leftDepth==0||rightDepth==0?leftDepth+rightDepth+1:Math.min(leftDepth,rightDepth)+1;
    }
}

简洁不少,巧妙之处在于leftDepth+rightDepth+1

2.使用dfs深度优先搜索

代码语言:javascript
复制
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
import javafx.util.Pair;
import java.lang.Math;

class Solution {
  public int minDepth(TreeNode root) {
    Queue<Pair<TreeNode, Integer>> stack = new LinkedList<>();
    if(root == null){
        return 0;
    }
    if (root != null) {
      stack.add(new Pair(root, 1));
    }

     int depth = Integer.MAX_VALUE;
    while (!stack.isEmpty()) {
      Pair<TreeNode, Integer> current = stack.poll();
      root = current.getKey();
      int current_depth = current.getValue();
      
        if(root.left==null&&root.right==null){
            depth = Math.min(depth, current_depth);
          
        }
        if(root.left!=null){
            stack.add(new Pair(root.left, current_depth + 1));
        }
        if(root.right!=null){
            stack.add(new Pair(root.right, current_depth + 1));
        }
      }
    
    return depth;
  }
}

关键点这里的int depth = Integer.MAX_VALUE;

3.BFS广度优先搜索 广度优先搜索,结合的数据结构是队列,代码如下:

代码语言:javascript
复制
class Solution {
  public int minDepth(TreeNode root) {
      if(root == null){
          return 0;
      }
      Queue<TreeNode> queue = new LinkedList<TreeNode>();
      queue.add(root);
      // root 本身就是一层,depth 初始化为 1
      int deep = 1;
      while(!queue.isEmpty()){
          int size = queue.size();
          for(int i=0;i < size;i++){
              TreeNode node = queue.poll();
              if(node.left==null&&node.right==null){
                  return deep;
              }
              if(node.left!=null){
                  queue.add(node.left);
              }
              if(node.right!=null){
                  queue.add(node.right);
              }
          }
          deep++;
      }
      return deep;
  }
}

这里只需要注意一个地方,就是这句 int size = queue.size();不能在for循环里使用queue.size();作为边界,因为每层size是变化的

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-01-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档