leetcode:Minimum Depth of Binary Tree【Python版】
1、类中递归调用添加self;
2、root为None,返回0
3、root不为None,root左右孩子为None,返回1
4、返回l和r最小深度,l和r初始为极大值;
1 # Definition for a binary tree node 2 # class TreeNode: 3 # def __init__(self, x): 4 # self.val = x 5 # self.left = None 6 # self.right = None 7 8 class Solution: 9 # @param root, a tree node 10 # @return an integer 11 def minDepth(self, root): 12 if root == None: 13 return 0 14 if root.left==None and root.right==None: 15 return 1 16 l,r = 9999,9999 17 if root.left!=None: 18 l = self.minDepth(root.left) 19 if root.right!=None: 20 r = self.minDepth(root.right) 21 if l<r: 22 return 1+l 23 return 1+r
转载于:https://www.cnblogs.com/CheeseZH/p/4034307.html
leetcode:Minimum Depth of Binary Tree【Python版】相关推荐
- 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 ...
- leetcode - Minimum Depth of Binary Tree
题目:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is th ...
- 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 ...
- [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 ...
- LeetCode | Minimum Depth of Binary Tree
题目:给定一个二叉树,找到其最小深度.最小深度是从根节点到最近叶节点的最短路径的节点数. 1 /** 2 * Definition for binary tree 3 * public class T ...
- leetcode --Minimum Depth of Binary Tree
找最短的到叶节点的长度: 一考虑广度优先搜索(使用队列,不用 recursive) class Solution { public:int minDepth(TreeNode* root) {if(! ...
- LeetCode 111. Minimum Depth of Binary Tree--Java, Python解法--二叉树最小高度--迭代,递归
题目地址:Minimum Depth of Binary Tree - LeetCode Given a binary tree, find its minimum depth. The minimu ...
- 【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——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
最新文章
- CSS中关于清除浮动的问题
- c语言动态链表creat函数,用create建立动态链表
- ai一个线段多个箭头_初学设计却分不清PS和AI?超详细的AI基础介绍包你一看即会!...
- 第三次学JAVA再学不好就吃翔(part58)--String和StringBuffer分别作为参数传递
- git语言包安装_Git分布式版本管理系统快速入门指南
- SAP “麻辣三人行”小厨房|用黑科技“烹制”HR数字料理
- 开源操作系统年度技术会议图文直播
- blog迁来迁去的......
- ASP.NET Core使用Session
- 【SOT】SiamFC代码笔记
- 主流web前端技术框架有哪些?
- 应用网易轻舟,德邦快递核心系统入选云原生应用十大优秀案例
- macOS SwiftUI 指示器组件规范之 01 液位指示器Level Indicators
- Python前世今生
- 高德地图和百度地图数据下载
- 基于微信小程序的个人管理软件
- 赛轮转债上市价格预测
- OpenCV每日函数 几何图像变换模块 (9) resize函数
- 微信小程序如何调用腾讯地图进行定位的简单方法
- arcgis建立拓扑验证拓扑
热门文章
- IPNC RDK5.0 make sysall 无法通过问题
- hdu2110(普通母函数)
- Bit-Z 项目评审中心开启首次投票币竞选
- Android mediaRecorder框架简述(一)
- Android MediaRecorder架构详解
- The new NDK support in Android Studio 1.3
- python里compile的作用_Python代码中compile()函数具有哪些功能呢?
- JZOJ 5711. 【北大夏令营2018模拟5.13】时间幻象
- JZOJ 5401. 【NOIP2017提高A组模拟10.8】Star Way To Heaven
- c语言和gtk实现的游戏,Ubuntu下用glade和GTK+开发C语言界面程序(一)