【leetcode】Minimum Depth of Binary Tree
题目简述:
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
解题思路:
# 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
min = 99999
def minDeep(self, root, cur):
if root == None:
self.min = 0
return
cur += 1
if root.left == None and root.right ==None:
if self.min >cur:
self.min = cur
return
if root.left != None:
self.minDeep(root.left, cur)
if root.right != None:
self.minDeep(root.right, cur)
def minDepth(self, root):
self.minDeep(root, 0)
return self.min
【leetcode】Minimum Depth of Binary Tree的更多相关文章
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【leetcode】Minimum Depth of Binary Tree (easy)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 【Leetcode】【Easy】Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)
这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...
- 【LeetCode】Maximum Depth of Binary Tree
http://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ public class Solution { public int max ...
- 【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 ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- LeetCode OJ Minimum Depth of Binary Tree 递归求解
题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...
- [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 ...
随机推荐
- C# 获取磁盘剩余空间
drive.TotalFreeSpace单位为bit,根据需要除以1024 drive同时可以可以获取磁盘分区容量等 //单位MB public static long GetHardDiskSpac ...
- MVC防止xss攻击 ——Html.AntiForgeryToken的AJAX提交
1.在Html表单里面使用了@Html.AntiForgeryToken()就可以阻止CSRF攻击. 2.相应的我们要在Controller中也要加入[ValidateAntiForgeryToken ...
- log4net位置与使用方法
<log4net> <appender name="RollingLogFileAppender" type="log4net.Appender.Rol ...
- 怎么把本地项目和远程git仓库相连通
1. 打开在你的项目文件夹,输入下面的命令 git init 输完上面的命令,文件夹中会出现一个.git文件夹,如下图所示,其他的的文件也会出现蓝色小问号的标志 2. 添加所有文件 git add . ...
- eclipse项目打包
一.直接在eclipse里export即可 二.如果需要打好的包可以直接使用,则需要修改包中的META-INF文件夹中的MANIFEST.MF文件,主要可能会涉及到的字段主要有以下两个. 1.Main ...
- Unity3D 动态改变地形
直接获取TerrainData进行修改即可 using System.Collections; using UnityEngine; using UnityEditor; public class D ...
- 兼容好的JS图片上传预览代码
转 : http://www.codefans.net/articles/1395.shtml 兼容好的JS图片上传预览代码 (谷歌,IE11) <html xmlns="http:/ ...
- 利用gcc自带的功能-fstack-protector检测栈溢出及其实现
最近又遇到了一个崩溃,栈回溯非常怪异. /lib/i386-linux-gnu/libc.so.(gsignal+0x4f) [0xb2b751df] /lib/i386-linux-gnu/libc ...
- SqlServer 常用函数
case .. when .. then .. else .. end
- linux磁盘空间查询
LINUX服务器查询 1. du -sch * 使用该命令查询当前目录下文件夹占用的空间的情况 2. df -hl 查询磁盘剩余空间 3. root权限 fdisk -l