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)
            
            
            

            
            
        
        
           

  • 相关阅读:
    MyEclipse中代码提醒功能
    oracle12c创建用户等问题
    java中的构造块、静态块等说明
    jquery中的get和post、ajax有关返回值的问题描述
    最大半连通子图 BZOJ 1093
    最小生成树计数 BZOJ 1016
    水平可见直线 BZOJ 1007
    分金币 BZOJ 3293
    游走 BZOJ 3143
    糖果 BZOJ 2330
  • 原文地址:https://www.cnblogs.com/kwangeline/p/5994790.html
Copyright © 2011-2022 走看看