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. English trip -- VC(情景课) 7 A Shopping 购物

    Words The clothes place a dress 长裙      short skirt 短裙 pants 裤子   /  trousers 长裤  / shorts 短裤 a shir ...

  2. 52 53django

    一.需要弄清楚的几个关键点: KEY: 1 Django项目无论多大,只是一个应用程序 2 地址栏发请求默认是GET请求 form表单可以发送get请求,也可以发送post请求 3 浏览器接受的响应体 ...

  3. Mac搭建个人服务器开发微信公众号

    个人电脑搭建小型服务器面临的一个重要问题就是网络问题,我把解决的过程简单记录一下,准备以后参考. 1. 内网穿透 由于个人所在网络是一个局域网,没有公网的IP,因此在公网是不能直接访问个人电脑的服务器 ...

  4. hdu-1907-反nim博弈

    John Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submis ...

  5. 关于vue Unexpected identifier 问题

    vue对于es6虽然自带babel转换 但是在index.html文件中并不会发生转换 因此在index.html中使用新的语法会导致低版本浏览器不识别代码因此报出Unexpected identif ...

  6. JS-图片控制-动画管理模块

    animateManage.js ;(function(window,document,undefined){ var _aniQueue = [], //动画队列 --- ani:动画,Queue: ...

  7. Error when clicking other button after displaying Popup window(转)

    原文地址:Error when clicking other button after displaying Popup window Hi, I'm developing a custom page ...

  8. 文件的移动,删除 rename remove unlink 函数

    int rename(const char *oldpath, const char *newpath); rename()  renames  a  file,  moving it between ...

  9. 1.strcpy使用注意

    void test1() { char string[10]; char *str1 ="0123456789"; strcpy(string,str1); } 上述代码问题? 字 ...

  10. Flask初级(九)flash与前台交互get详解

    Project name :Flask_Plan templates:templates static:static @app.route('/') def hello_world(): return ...