1.编辑器

我使用的是win10+vscode+leetcode+python3
环境配置参见我的博客:
链接

2.第一百一十一题

(1)题目
英文:
Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

Note: A leaf is a node with no children.

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

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

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/minimum-depth-of-binary-tree

(2)解法
① 递归
(耗时: 60ms,内存:15.6M)

class Solution:def minDepth(self, root: TreeNode) -> int:if(not root):return 0if(not root.left):return self.minDepth(root.right)+1if(not root.right):return self.minDepth(root.left)+1return min(self.minDepth(root.left),self.minDepth(root.right))+1

② DFS
(耗时: 56ms,内存:14.9M)

class Solution:def minDepth(self, root: TreeNode) -> int:if(not root):return 0queue = collections.deque([(1,root)])while(queue):depth,node=queue.popleft()if(not node.left and not node.right):return depthif(node.left):queue.append((depth+1,node.left))if(node.right):queue.append((depth+1,node.right))

leetcode python3 简单题111. Minimum Depth of Binary Tree相关推荐

  1. leetcode python3 简单题104. Maximum Depth of Binary Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零四题 (1)题目 英文: Given a binary tree, find ...

  2. LeetCode: 111. Minimum Depth of Binary Tree

    题目 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bina ...

  3. LeetCode 111. Minimum Depth of Binary Tree

    原题 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the s ...

  4. LeetCode 111. Minimum Depth of Binary Tree (二叉树最小的深度)

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  5. 111. Minimum Depth of Binary Tree

    1.问题描述 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along t ...

  6. 111. Minimum Depth of Binary Tree 二叉树的最小深度

    给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], ...

  7. LeetCode 111. Minimum Depth of Binary Tree--Java, Python解法--二叉树最小高度--迭代,递归

    题目地址:Minimum Depth of Binary Tree - LeetCode Given a binary tree, find its minimum depth. The minimu ...

  8. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  9. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

最新文章

  1. cp: /usr/bin/chromedriver: Operation not permitted
  2. 面试时真能“坦白从宽”?
  3. Java Lambda表达式初探
  4. StructLayout(LayoutKind.Sequential)(转)
  5. oracle 快速入门之第一章 数据库基础
  6. Au 音频效果参考(合集)
  7. RecyclerView 实现多种布局(上半部Gridview样式,下半部Listview样式)以及多种数据类型实现不同布局
  8. 微信小程序:老人疯狂裂变引流视频推广微信小程序
  9. 2021年王道数据结构课后题
  10. Python 命名规范
  11. JAVA Swing万年历
  12. javascript飞机大战-----007爆炸效果
  13. win10解决设置默认打开方式不生效问题
  14. Java后端使用Freemarker导出word文档的各种细节
  15. 提交到GitHub错误:src refspec 分支名 does not match any
  16. RHCE投资失败!需要及时止损
  17. 2D横版摩托游戏源码
  18. 财政预算绩效优秀案例
  19. 重庆:智能网联汽车驶入“快车道”,中国“底特律”走向复兴?
  20. h5封装table表格 vue

热门文章

  1. php球鞋,适合宽脚选手的球鞋有哪些“宽型脚”必备的实战利器推荐
  2. python xpath定位元素方法_python--通过xpath相对节点位置查找元素(续)
  3. AcWing1085.不要62(数位DP)题解
  4. 5-3 面向可维护性的构造技术
  5. 图像处理——在Python中使用OpenCV显示图像
  6. 实例分割——转置卷积的学习笔记
  7. input中radio对象的使用、获取方法
  8. 图像的放大与缩小——双线性插值放大与均值缩小
  9. libtorch下tensor与img的互相转换
  10. IntellijIDEA配置Tomcat