# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
# def minDepth(self, root):
# """
# :type root: TreeNode
# :rtype: int
# """
# # Recursion
# if root is None:
# return 0
# ld = self.minDepth(root.left)
# rd = self.minDepth(root.right)
# if ld != 0 and rd != 0:
# # handle 0 case!
# return 1 + min(ld, rd)
# return 1 + ld +rd
def minDepth(self, root):
# BFS
if root is None:
return 0
queue = [root]
depth, rightMost = 1, root
while len(queue) > 0:
node = queue.pop(0)
if node.left is None and node.right is None:
break
if node.left is not None:
queue.append(node.left)
if node.right is not None:
queue.append(node.right)
if node == rightMost:
# reach the current level end
depth += 1
if node.right is not None:
rightMost = node.right
else:
rightMost = node.left
return depth
python-leetcode面试题解之第111题二叉树的最小深度-题解.zip
需积分: 1 93 浏览量
2024-04-29
08:17:43
上传
评论
收藏 1KB ZIP 举报
Mopes__
- 粉丝: 1042
- 资源: 319
最新资源
- JAVA实现Modbus RTU或Modbus TCPIP案例.zip
- 基于YOLOv8的FPS TPS AI自动锁定源码+使用步骤说明.zip
- JAVA实现Modbus RTU或Modbus TCPIP案例.zip
- 基于yolov8+streamlit的火灾检测部署源码+模型.zip
- 测试aaaaaaabbbbb
- VID20240521070643.mp4
- Android系统原理与开发学习要点详解-培训课件.zip
- 部署yolov8的tensorrt模型支持检测分割姿态估计的C++源码+部署步骤.zip
- 以简单、易用、高性能为目标、开源的时序数据库,支持Linux及Windows, Time Series Database.zip
- python-leetcode面试题解之第198题打家劫舍-题解.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈