zoukankan      html  css  js  c++  java
  • 【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)
            
            
            

            
            
        
        
           

  • 相关阅读:
    spring4-2-bean配置-2-属性注入细节
    内存管理tcmalloc
    并发视频,怎么hold住高并发
    struts总结
    最近想玩的一个方向
    虚拟化技术漫谈
    陈硕的博客
    木铎
    安全编程资源-苹果
    POOL
  • 原文地址:https://www.cnblogs.com/kwangeline/p/5994790.html
Copyright © 2011-2022 走看看