【LeetCode练习题】Minimum Depth of Binary Tree
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.
和上一题对应,求二叉树的最小深度。
解题思路:
参考上一题Maximun Depth of Binary Tree中最后那个极短的解法。
另外需要判断一下递归返回0的时候的结果不可取,因为不是叶节点。
代码如下:
class Solution {
public:
int minDepth(TreeNode *root) {
if(!root)
return ;
int l = minDepth(root->left);
int r = minDepth(root->right);
if(l * r != )
return min(l,r)+;
else if(l == )
return r+;
else
return l+;
}
};
【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] 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 ...
- [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum 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 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- 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 ...
- [leetcode] 5. Minimum Depth of Binary Tree
二叉树基本功练习题,题目如下: Given a binary tree, find its minimum depth. The minimum depth is the number of node ...
- leetcode 111 minimum depth of binary tree
problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...
- 【leetcode】Minimum Depth of Binary Tree
题目简述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...
随机推荐
- 【HDU1162】Eddy's picture(MST基础题)
很基础的点坐标MST,一不留神就AC了, - - !! #include <iostream> #include <cstring> #include <cstdlib& ...
- 炫酷吊炸天的nodeppt
由于要做一个关于node的分享,要准备写一个ppt方便就行交流.之前用的比较多的是slides(http://www.slides.com),最近知道了一个node写的工具,可以生成ppt,号称很强大 ...
- easyhadoop初识以及各种问题
版本:easyhadoop 1.2.1,操作系统:Centos 6.4: 首先,我要说的是要用对版本,这点很重要.我使用ubuntu12.04 64bit 的虚拟机是不行的,安装直接报错.所以就下载了 ...
- Thinkphp将中文年份转换为数字年份的问题
今天遇到一个问题:想将中文年份转换为数字年份,例如:"二零一六"-->'2016'. 在网上搜了一下,没找到可以直接处理的函数(也许是我搜索信息的能力有限吧>_< ...
- hdu 5429 Geometric Progression(存个大数模板)
Problem Description Determine whether a sequence is a Geometric progression or not. In mathematics, ...
- C#开发客户端、JAVA和tomcat开发服务端
hessian入门,Hello和文件上传范例,C#客户端+Java Tomcat后台 2.Hello范例1)后台--定义Java接口:package org.migle.hessian; public ...
- pyqt 右击+指定位置点击例子学习
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' import sys from PyQt4.QtCore impor ...
- sprig——jar包
Struts. Hibernate.Spring这类的框架给我们开发带来非常大的好处,让我们更加快速.有效的开发.所以我们在开发中通常都会用到各种框架,每个框架 都有很多jar包,每个jar都有各自不 ...
- Staple: Complementary Learners For Real-time Tracking Tracking
转载注明出处: http://www.cnblogs.com/sysuzyq/p/6169414.html By 少侠阿朱 讨论班上的PPT 1.同学大家好.今天给大家讲一篇单目标跟踪的论文,方法比较 ...
- wifi_uplink脚本分析
~ >: vim apps/tools/wifi_uplink #!/bin/sh # Copyright (C) 2012 GuoWenxue <guowenxue@gmail.com ...