[leetcode] 5. 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 binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void minDepth(TreeNode *temp, int count, int &min)
{
if (temp != NULL)
{
if (temp->left == NULL && temp->right == NULL)
{
min = min > count ? count : min;
} minDepth(temp->left, count + 1, min);
minDepth(temp->right, count + 1, min);
}
}
int minDepth(TreeNode *root) {
if (root == NULL)
{
return 0;
}
int min = 2147483647;
minDepth(root, 1, min);
return min;
}
};
就是这样,拿count来计数,然后把min的地址传参进去,每到底部叶子的时候判断一次大小。
[leetcode] 5. 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 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 ...
- 【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 ...
随机推荐
- 黄聪:Wordpress程序Mysql查询导致CPU100%,页面错误增量飙高解决方案
最近做的网站,访客一多,CPU就会飙到100%,找了网上很多解决方案,用了下面的方法进行优化: 1.mysql开启慢查询方法: 在my.ini的[mysqld]添加如下语句: log-slow-que ...
- 峰Spring4学习(6)spring AOP的应用例子
一.AOP简介: 二.AOP实例: 三.使用的例子 需求:在student添加的前后,打印日志信息: 0)spring AOP需要引用的jar包: 1)StudentService.java接口: p ...
- [转][javascript]判断传入参数
// IE 下 name 都是 undefined ,这里手动赋值 Number.name="Number"; //String.name="String"; ...
- [转]oracle 12c 中的分页子句
转自:http://blog.itpub.net/271063/viewspace-1061279/ -- 连接数据库 创建测试用户-- Connected to Oracle Database 12 ...
- springboot 2.0 自动装配原理 以redis为例
当面试管问你springboot 和 普通spring 有什么区别? 您还在回答: 简化了配置 ,内置tomcat 等等 吗 ? 那只是皮毛, 最重要的还是自动化配置.一起来了解一下 第一步: 第二步 ...
- centos7.3部署django用uwsgi和nginx[亲测可用]
现在nginx nginx version: nginx/1.10.2 uwsgi 2.0.17 django2.0.5 都已经完成完毕,那么开始 uwsgi 配置 uwsgi支持ini.xml等多种 ...
- Windows下测量cpu利用率的类
方法参考网上的一段代码,把它改成了类以方便调用,很简单,但是可以一用,哈哈. cpu_usage.h #include <windows.h> class CpuUsage { publi ...
- JS判断不能为空实例代码
JS判断不能为空实例代码.分享在此. 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" " ...
- IOS AudioServicesPlaySystemSound 后台锁屏播放
AudioServicesPlaySystemSound 想在锁屏后台播放报警提示音. 添加了UIBackgroundModes,audio,官方审核不通过! IOS的闹钟是怎么实现的,锁屏不能播放声 ...
- TDataset.CopyFields
Description Often when manipulating datasets with similar structures, you need to copy the records f ...