111. 二叉树的最小深度
BFS
从根节点 BFS搜索 其他节点 加入队列,然后依次出队,判断子节点是否存在 不存在则说明搜索到叶子节点,然后返回其深度。
class Solution:
def minDepth(self, root: TreeNode) -> int:
if not root:
return 0
que = [[root,1]]
while que:
node ,num = que.pop(0)
if not node.left and not node.right:
return num
if node.left:
que.append([node.left,num+1])