#-*- 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)

【leetcode❤python】 111. Minimum Depth of Binary Tree的更多相关文章

  1. 【LeetCode】111. Minimum Depth of Binary Tree (2 solutions)

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  2. 【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

  3. 【一天一道LeetCode】#111. Minimum Depth of Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【LeetCode】111 - Minimum Depth of Binary Tree

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  5. 【easy】111. Minimum Depth of Binary Tree求二叉树的最小深度

    求二叉树的最小深度: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...

  6. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  7. [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  8. Leetcode 111 Minimum Depth of Binary Tree 二叉树

    找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...

  9. [LeetCode]题解(python):111 Minimum Depth of Binary Tree

    题目来源 https://leetcode.com/problems/minimum-depth-of-binary-tree/ Given a binary tree, find its minim ...

随机推荐

  1. Tomcat上的项目部署到WebLogic上の注意事项

    1.修改web.xml: <!-- <display-name>weboutweb</display-name> --> <!-- 注释掉 display-n ...

  2. PAT乙级 1003. 我要通过!(20)

    答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”. 得到“答案正确”的条件是: 1. ...

  3. zw版_Halcon图像库delphi接口文件

    zw版_Halcon图像库delphi接口文件 Halcon图像库delphi接口文件,根据安装时用户设置的文件目录不同,会有所差异,笔者一般安装在delphi的import目录下.     参见:& ...

  4. yii2开启session

    1.在写入session的页面. use yii\web\Session;$session = new Session;$session->open(); 2.在获取session的页面 use ...

  5. UIViewController卸载过程(ios6.0以后)

    在ios6.0以后,废除了viewWillUnload方法和viewDidUnload方法. 在ios6以后,当收到didReceiveMemoryWarning消息调用之后,程序会自动调用didRe ...

  6. 获取CPUID等

    unit CommonUnit; interface uses Windows, SysUtils, DateUtils; Const CPUVendorIDs: .. ] of string = ( ...

  7. 鸟哥的linux私房菜学习记录之认识系统服务(daemons)

  8. django ORM model filter 条件过滤,及多表连接查询、反向查询,某字段的distinct

    版权归作者所有,任何形式转载请联系作者.作者:petanne(来自豆瓣)来源:https://www.douban.com/note/301166150/ 1.多表连接查询:感觉django太NX了. ...

  9. python实现指定目录下批量文件的单词计数:串行版本

    直接上代码. 练习目标: 1.  使用 Python 面向对象的方法封装逻辑和表达 : 2.  使用异常处理和日志API : 3.  使用文件目录读写API : 4.  使用 list, map, t ...

  10. 兼容IE, Chrome的ajax function

    gAjax.js var gAjax = (function () { /* paramObj:{ url: request url, method: GET or POST, encode: cha ...