leetcood学习笔记-111-二叉树的最小深度
题目描述:

第一次提交:
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-二叉树的最小深度的更多相关文章
- Java实现 LeetCode 111 二叉树的最小深度
111. 二叉树的最小深度 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,nu ...
- 【LeetCode】111. 二叉树的最小深度
111. 二叉树的最小深度 知识点:二叉树,递归 题目描述 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明:叶子节点是指没有子节点的节点. 示例 输入 ...
- leetcode 111. 二叉树的最小深度
题目描述: 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null, ...
- [LeetCode 111] - 二叉树的最小深度 (Minimum Depth of Binary Tree)
问题 给出一棵二叉树,找出它的最小深度. 最小深度是指从根节点沿着最短路径下降到最近的叶子节点所经过的节点数. 初始思路 不难看出又是一个需要层次遍历二叉树的题目,只要在112基础上作出简单修改即可得 ...
- 每日一题-——LeetCode(111)二叉树的最小深度
题目描述: 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 思路一: 把每一层的结点加入到队列,每一层i+1,到下一层时,把上一层在队列中的结点都弹出,按从 ...
- LeetCode【111. 二叉树的最小深度】
最小深度,看起来很简单,就是左右节点的深度最小值 定义一个函数,计算其深度 class Solution { public int minDepth(TreeNode root) { if(root ...
- leetcode 111二叉树的最小深度
使用深度优先搜索:时间复杂度O(n),空间复杂度O(logn) /** * Definition for a binary tree node. * struct TreeNode { * int v ...
- leetcood学习笔记-226- 翻转二叉树
题目描述: 第一次提交: class Solution(object): def invertTree(self, root): """ :type root: Tree ...
- Leecode刷题之旅-C语言/python-111二叉树的最小深度
/* * @lc app=leetcode.cn id=111 lang=c * * [111] 二叉树的最小深度 * * https://leetcode-cn.com/problems/minim ...
随机推荐
- 2019-8-31-How-to-parse-version-range
title author date CreateTime categories How to parse version range lindexi 2019-08-31 16:55:58 +0800 ...
- MySQL索引与事务
MySQL索引与事务 链接:https://pan.baidu.com/s/1ANGg3Kd_28BzQrA5ya17fQ 提取码:ekpy 复制这段内容后打开百度网盘手机App,操作更方便哦 1.索 ...
- #ifdef 宏定义一个main编译客户端服务端2套代码
#include <iostream> using namespace std; #include "ProWrapper.h" #include "Serv ...
- qfile读取txt文件
QFile f("D:\\测试数据\\单波束数据\\灯浮.TGT"); if (!f.open(QIODevice::ReadOnly|QIODevice::Text))//打开指 ...
- vue 使用Better-Scroll
注意点 1. 外层容器wrapper要设置高度,并且overflow:hidden. 2. wrapper里面的需要一个div包裹所有内容 3. 样式成功 4. 以上就是可以滚动的情况,wrappe ...
- css 垂直居中、水平居中
在父元素.子元素未知的情况下居中有两种方法: 第一种方法: .partent{ display:flex; justify-content:center; align-items:center; } ...
- Java虚拟机(一)
一.Java发展历程 Java之父,James Gosling博士 时间 事件 1991年4月 James Gosling博士领导的Green Project启动,java语言前身Oak启动 1995 ...
- springMVC配置文件 的约束
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- MySql5.7默认生成的密码无法正常登陆
1.修改 /etc/my.cnf,在 [mysqld] 小节下添加一行:skip-grant-tables=1 这一行配置让 mysqld 启动时不对密码进行验证 2.重启 mysqld 服务:sys ...
- php-验证码类-PDO类-缩略图类
Verify.class.php 验证码类 <?php class Verify{ const VERIFY_TYPE_NUM=1; const VERIFY_TYPE_EN=2; const ...