From 0990dfe810af83c4ed41c605fd165dad1f76645b Mon Sep 17 00:00:00 2001 From: shuwen Date: Thu, 19 Aug 2021 17:00:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200102.=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md=20=20?= =?UTF-8?q?=E5=85=B3=E4=BA=8E111.=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6=E7=9A=84Python=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 32 ++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index dc386684..109ed70f 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1562,6 +1562,8 @@ JavaScript: # 111.二叉树的最小深度 +题目地址:https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/ + 相对于 104.二叉树的最大深度 ,本题还也可以使用层序遍历的方式来解决,思路是一样的。 **需要注意的是,只有当左右孩子都为空的时候,才说明遍历的最低点了。如果其中一个孩子为空则不是最低点** @@ -1597,7 +1599,35 @@ public: Java: -Python: +Python 3: + +```python 3 +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def minDepth(self, root: TreeNode) -> int: + if root == None: + return 0 + + #根节点的深度为1 + queue_ = [(root,1)] + while queue_: + cur, depth = queue_.pop(0) + + if cur.left == None and cur.right == None: + return depth + #先左子节点,由于左子节点没有孩子,则就是这一层了 + if cur.left: + queue_.append((cur.left,depth + 1)) + if cur.right: + queue_.append((cur.right,depth + 1)) + + return 0 +``` Go: