zoukankan      html  css  js  c++  java
  • 【leetcode❤python】 111. Minimum Depth of Binary Tree

    #-*- coding: UTF-8 -*-
    # 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)
            
            
           

  • 相关阅读:
    DockerFile体系结构
    Nignx(二) server_name 规则
    解决Redis分布式锁——死锁问题
    redis缓存穿透,缓存击穿,缓存雪崩原因+解决方案
    Docker_Linux
    正则例子
    部属流程
    Mysql insert语句的优化
    MySQL innodb_fast_shutdown参数讲解
    MySQL技术内幕InnoDB存储引擎(表&索引算法和锁)
  • 原文地址:https://www.cnblogs.com/kwangeline/p/6059535.html
Copyright © 2011-2022 走看看