1、类中递归调用添加self;

2、root为None,返回0

3、root不为None,root左右孩子为None,返回1

4、返回l和r最小深度,l和r初始为极大值;

 # Definition for a  binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param root, a tree node
# @return an integer
def minDepth(self, root):
if root == None:
return 0
if root.left==None and root.right==None:
return 1
l,r = 9999,9999
if root.left!=None:
l = self.minDepth(root.left)
if root.right!=None:
r = self.minDepth(root.right)
if l<r:
return 1+l
return 1+r

leetcode:Minimum Depth of Binary Tree【Python版】的更多相关文章

  1. leetcode Minimum Depth of Binary Tree python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  2. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  3. LeetCode: Minimum Depth of Binary Tree 解题报告

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

  4. [LeetCode] 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. LeetCode - Minimum Depth of Binary Tree

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

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

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

  7. leetcode Maximum Depth of Binary Tree python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  8. LeetCode Minimum Depth of Binary Tree 找最小深度(返回最小深度)

    题意:找到离根结点最近的叶子结点的那一层(设同一层上的结点与根结点的距离相等),返回它所在的层数. 方法有: 1.递归深度搜索 2.层次搜索 方法一:递归(无优化) /** * Definition ...

  9. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

随机推荐

  1. 20170706xlVBA根据工资汇总表生成个人工资条

    Sub NextSeven20170706001() Application.ScreenUpdating = False Application.DisplayAlerts = False Appl ...

  2. 5-13 Rspec实际; validates处理Errors, TDD, 单元测试和验收测试,capybara

    validates处理验证错误:详见ActiveModel::Errors文档 一,errors ActiveModel::Errors的实例包含所有的❌.每个错误:key是每个属性的name, va ...

  3. Confluence 6 使用 LDAP 授权连接到 Confluence 内部目录

    希望连接一个内部目录但是使用 LDAP 检查登录授权: 在屏幕的右上角单击 控制台按钮 ,然后选择 General Configuration 链接. 单击左侧面板上面的 用户目录(User Dire ...

  4. Linux 下载最新kubectl版本的命令:

    ubuntu centos下通用 第一步.下载最新版本的命令: curl -LO https://storage.googleapis.com/kubernetes-release/release/$ ...

  5. python-day15函数递归

    1.递归: 在函数内,调用自己.  (技巧: 每次调用时,函数前面需加上return,这样返回值就可以一层一层 的返回去) #def age(n):#    if n == 1:#        re ...

  6. How do you add?(递推)

    题意:求将n分为k个数相加的种数. 如:n=20,k=2,则可分为: 0+20=20 1+19=20 2+18=20 ....... 20 +0=20 共21种方案. 解析:令f(n,m)表示将n分为 ...

  7. EBS R12使用接口表往已存在的供应商地址下创建新的地点

    在供应商 "测试供应商A" 下已经有了两个地址,分别为 "地址A","地址B",现在由于某些原因,需要在地址A下面创建新的地点. 由于业务需 ...

  8. CNN autoencoder 进行异常检测——TODO,使用keras进行测试

    https://sefiks.com/2018/03/23/convolutional-autoencoder-clustering-images-with-neural-networks/ http ...

  9. 铺音out2

    1◆ 忘记的 ed t   d     du dʒ dge   si ʒ su         ph f gh     ck k ch gh   2◆ 整理 success

  10. Idea热部署jrebel失败

    Idea热部署jrebel