leetcode?python 111. Minimum Depth of Binary Tree
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode?python 111. Minimum Depth of Binary Tree相关的知识,希望对你有一定的参考价值。
# 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):
depthList=[]
def minDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
self.depthList=[]
if root==None:return 0
self.depthList.append(1)
self.dfs(root)
return min(self.depthList)
def dfs(self,root):
curdepth=self.depthList[-1]
if root.right!=None or root.left!=None:
self.depthList.pop()
else:return
if root.left!=None:
dep=curdepth+1
self.depthList.append(dep)
self.dfs(root.left)
if root.right!=None:
dep=curdepth+1
self.depthList.append(dep)
self.dfs(root.right)
以上是关于leetcode?python 111. Minimum Depth of Binary Tree的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode第111题—二叉树的最小深度—Python实现