题目地址:Minimum Depth of Binary Tree - LeetCode


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.

Example:

Given binary tree [3,9,20,null,null,15,7],

    3/ \9  20/  \15   7

这道题目是计算二叉树最小高度,可以用迭代或者递归来做。

递归解法如下。
Python解法如下:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = Noneclass Solution:def minDepth(self, root: TreeNode) -> int:def helper(root, height):if root == None:return height-1if root.left == None and root.right == None:return heightelif root.left == None:return helper(root.right, height+1)elif root.right == None:return helper(root.left, height+1)return min(helper(root.left, height+1), helper(root.right, height+1))return helper(root, 1)

迭代的解法如下。

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = Noneclass Solution:def minDepth(self, root: TreeNode) -> int:if root is None:return 0stack=[(1,root)]height=1while stack!=[]:depth,root=stack.pop(0)child=[root.left,root.right]if not any(child):height=max(depth,height)return heightfor i in child:if i is not None:stack.append((depth+1,i))return height

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

  1. 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 ...

  2. 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 ...

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

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

  4. LeetCode: 111. Minimum Depth of Binary Tree

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

  5. leetcode python3 简单题111. Minimum Depth of Binary Tree

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

  6. 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 ...

  7. leetcode:Minimum Depth of Binary Tree【Python版】

    1.类中递归调用添加self: 2.root为None,返回0 3.root不为None,root左右孩子为None,返回1 4.返回l和r最小深度,l和r初始为极大值: 1 # Definition ...

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

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

  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. SSM框架整合(Spring+SpringMVC+MyBatis)
  2. 7.串口操作之API篇 GetCommMask SetCommMask WaitCommEvent
  3. 886 微型计算机原理,886微型计算机原理及应用.pdf
  4. ConfigurableListableBeanFactory
  5. Lua字符串及模式匹配
  6. c语言三个月兴业,c语言输出2015年日历,要求三个月在一行...
  7. hihocoder 1457 : 后缀自动机四·重复旋律7(后缀自动机+拓扑序BFS)
  8. 待续--著名软件公司笔试算法题:122345排列组合
  9. hsqldb 的简单使用
  10. 语义表示——神经网络与深度学习
  11. html动画人物走路,动画人物走路教程
  12. YAPI自动生成接口文档,解放测试人生产力...
  13. 22牛客多校5 - Don‘t Starve(DP,依靠边更新端点)
  14. linux ubuntu动物,[趣闻]Ubuntu各大发行版的动物代号
  15. CC2530定时器1使能
  16. 计算机科学排行榜2018年,2018本科计算机专业社会影响力排行榜!电子科大实至名归...
  17. Java模拟实现ATM系统
  18. 【已解决】 com.rabbitmq.client.ShutdownSignalException: connection error; protocol meth
  19. 《安全周报》2011年7月第3期
  20. 【水滴石穿】mobx-todos

热门文章

  1. java ado连接mysql_【转】Java JDBC对应C# ADO连接数据库之区别
  2. 小程序 a标签_微慕WordPress小程序增强版V2.0新版上线
  3. 基因 ID 匹配利器
  4. 零基础入门学习Python(36) 类和对象:继承
  5. Python使用matplotlib可视化小提琴图、seaborn中的violinplot函数可视化多分类变量的小提琴图(Violin Plot)
  6. R语言使用xgboost构建回归模型:vtreat包为xgboost回归模型进行数据预处理(缺失值填充、缺失值标识、离散变量独热onehot编码)、构建出生体重的xgboost模型回归模型
  7. 运维管理工具+chef+puppet+ansible+SaltStack
  8. 安装没有在R包仓库CRAN上托管的R包
  9. Python控制流:顺序结构、分支结构、循环结构+for、if ... else、while、if... elif...elif..else、if
  10. 大卫·史文森+耶鲁模式+得意弟子张磊