题目描述:

第一次提交:

class Solution(object):
def minDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root:
return 0
if root.left and root.right:
return min(self.minDepth(root.left)+1,self.minDepth(root.right)+1)
if not root.left and root.right:
return self.minDepth(root.right)+1
if not root.right and root.left:
return self.minDepth(root.left)+1
if not root.left and not root.right:
return 1

优化后:

class Solution(object):
def minDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root: return 0
if not root.left or not root.right:
return 1 + max(self.minDepth(root.right), self.minDepth(root.left))
else:
return 1 + min(self.minDepth(root.right), self.minDepth(root.left))

方法二:广度优先 O(N)

from collections import deque
class Solution:
def minDepth(self, root):
if not root:
return 0
else:
node_deque = deque([(1, root),]) while node_deque:
depth, root = node_deque.popleft()
children = [root.left, root.right]
if not any(children):
return depth
for c in children:
if c:
node_deque.append((depth + 1, c))

leetcood学习笔记-111-二叉树的最小深度的更多相关文章

  1. Java实现 LeetCode 111 二叉树的最小深度

    111. 二叉树的最小深度 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,nu ...

  2. 【LeetCode】111. 二叉树的最小深度

    111. 二叉树的最小深度 知识点:二叉树,递归 题目描述 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明:叶子节点是指没有子节点的节点. 示例 输入 ...

  3. leetcode 111. 二叉树的最小深度

    题目描述: 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null, ...

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

    问题 给出一棵二叉树,找出它的最小深度. 最小深度是指从根节点沿着最短路径下降到最近的叶子节点所经过的节点数. 初始思路 不难看出又是一个需要层次遍历二叉树的题目,只要在112基础上作出简单修改即可得 ...

  5. 每日一题-——LeetCode(111)二叉树的最小深度

    题目描述: 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 思路一: 把每一层的结点加入到队列,每一层i+1,到下一层时,把上一层在队列中的结点都弹出,按从 ...

  6. LeetCode【111. 二叉树的最小深度】

    最小深度,看起来很简单,就是左右节点的深度最小值 定义一个函数,计算其深度 class Solution { public int minDepth(TreeNode root) { if(root ...

  7. leetcode 111二叉树的最小深度

    使用深度优先搜索:时间复杂度O(n),空间复杂度O(logn) /** * Definition for a binary tree node. * struct TreeNode { * int v ...

  8. leetcood学习笔记-226- 翻转二叉树

    题目描述: 第一次提交: class Solution(object): def invertTree(self, root): """ :type root: Tree ...

  9. Leecode刷题之旅-C语言/python-111二叉树的最小深度

    /* * @lc app=leetcode.cn id=111 lang=c * * [111] 二叉树的最小深度 * * https://leetcode-cn.com/problems/minim ...

随机推荐

  1. MySQL数据库企业级应用实践(多实例源码编译)

    MySQL数据库企业级应用实践(多实例源码编译) 链接:https://pan.baidu.com/s/1ANGg3Kd_28BzQrA5ya17fQ 提取码:ekpy 复制这段内容后打开百度网盘手机 ...

  2. 1、cmd中检测远程的ip和端口是否处于监听状态

    一.使用 ping 命令测试远程的ip是否可连通 cmd  (右键 管理员角色) ---  ping   IP 二.使用 telnet 测试远程某一个ip的端口是否开放 1.为了安全起见,window ...

  3. 阿里linux-Centos各版本下载

    https://mirrors.aliyun.com/centos/7/isos/x86_64/ Index of /centos/7/isos/x86_64/ ../ 0_README.txt 16 ...

  4. 【leetcode】969. Pancake Sorting

    题目如下: Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.len ...

  5. leetcode-163周赛-1262-可被3整除的最大和

    题目描述: 方法一:动态规划 O(N) class Solution: def maxSumDivThree(self, nums: List[int]) -> int: dp = [0, -1 ...

  6. python 爬取拉勾网

    import requestsimport randomimport timeimport osimport csvimport pandas as pdreq_url = 'https://www. ...

  7. javascript实现获取指定精度的上传文件的大小简单实例

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. 牛客网暑期ACM多校训练营(第五场) Agpa (最大化平均值)

    题目大意: 给定 n 门课以及它们的学分和绩点,定义总绩点是所有课的加权平均数,给定一个数 k, 你可以删除最多 k 门课,求你的总绩点最大能到多少 分析: 上面是牛客的官方题解,其实就是移项, 然后 ...

  9. linux浏览器,邮件客户端,输入法,双屏设置,应用软件,gnome-screenshot/scrot -s截图,office

    搜狗输入法linux版:http://pinyin.sogou.com/linux/help.php win/linux同时支持比较好用的浏览器:maxthon,firefox,maxthon,ope ...

  10. 一:unittest框架配合selenium工具之CSS_selector定位。

    做了自动化测试这么久了,一直没有梳理到元素定位这一块的内容,其重要性不言而喻.趁着周末有时间,梳理一下. 1,通过id定位 driver.find_element_by_css_selector(&q ...