Leetcode 111
二叉树最小深度
class Tree(object):
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
def minDepth(root:Tree):
if root == None:
return 0
depth = 1
temp = [(depth, root)]
while len(temp) > 0:
depth_temp, root = temp[0]
temp.pop(0)
if root.left == None and root.right == None:
return depth_temp
else:
depth_temp += 1
if root.left != None:
temp.append((depth_temp, root.left))
if root.right != None:
temp.append((depth_temp, root.right))
test = Tree(1, Tree(2, Tree(4), Tree(5)), Tree(3, Tree(4), Tree(5)))
print(minDepth(test))