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.
SOLUTION 1:
递归
这种递归解法更简单。因为在本层递归中不需要考虑左右子树是否为NULL的情况。因为我们直接
把 null 设置为返回一个最大值,这样的话,如果出现空子树,它不会影响最小值。但是如果左
右均为空,则应返回1(即是仅仅为根节点)
而且这种做法更加合理。 因为如果是空树,应该是无法到达才是。这时就应该将值设置为最大。
// SOLUTION 1:
public int minDepth1(TreeNode root) {
/*
主页君认为,在这应该是属于未定义行为,这里我们定义为MAX会比较好,因为
null就是取不到任何节点,没有path,不应该将最小值定为0.
*/
if (root == null) {
return 0;
} return dfs(root);
} /*
* The Recursion Version:
* 这种递归解法更简单。因为在本层递归中不需要考虑左右子树是否为NULL的情况。因为我们直接
把 null 设置为返回一个最大值,这样的话,如果出现空子树,它不会影响最小值。但是如果左
右均为空,则应返回1(即是仅仅为根节点) 而且这种做法更加合理。 因为如果是空树,应该是无法到达才是。这时就应该将值设置为最大。
* */
public int dfs(TreeNode root) {
if (root == null) {
return Integer.MAX_VALUE;
} // The base case: the root is a leaf.
if (root.left == null && root.right == null) {
return 1;
} return Math.min(dfs(root.left), dfs(root.right)) + 1;
}
SOLUTION 2:
使用level traversal会更快。因为我们要的是最短深度。当达到叶子节点 就可以直接退出了。
// SOLUTION 2:
// Level Traversal:
public int minDepth(TreeNode root) {
/*
主页君认为,在这应该是属于未定义行为,这里我们定义为MAX会比较好,因为
null就是取不到任何节点,没有path,不应该将最小值定为0.
*/
if (root == null) {
return 0;
} int level = 0; Queue<TreeNode> q = new LinkedList<TreeNode>();
q.offer(root); while (!q.isEmpty()) {
int size = q.size();
level++;
for (int i = 0; i < size; i++) {
TreeNode cur = q.poll(); if (cur.left == null && cur.right == null) {
return level;
} if (cur.left != null) {
q.offer(cur.left);
} if (cur.right != null) {
q.offer(cur.right);
}
}
} return 0;
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/MinDepth_1218_2014.java
LeetCode: Minimum Depth of Binary Tree 解题报告的更多相关文章
- 【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...
- 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 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...
- LeetCode 104 Maximum Depth of Binary Tree 解题报告
题目要求 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- 【LeetCode】655. Print Binary Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- LeetCode - Minimum Depth of Binary Tree
题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...
- [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】965. Univalued Binary Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...
随机推荐
- libgdx游戏引擎教程
第一讲:libgdx游戏引擎教程(一)性能优良的游戏引擎—libgdx http://www.apkbus.com/android-57355-1-1.html 第二讲: libgdx游戏引擎教程(二 ...
- JavaScript 函数入门略解
1.JavaScript 函数语法函数就是包裹在花括号中的代码块,前面使用了关键词 function: function functionname() { 这里是要执行的代码 } 当调用该函数时,会执 ...
- Vue.js——60分钟快速入门 开发· webpack 中文文档
转载于:http://www.cnblogs.com/keepfool/p/5619070.html http://www.css88.com/doc/webpack2/guides/get-star ...
- 查看/修改Linux时区和时间
一.时区 1. 查看当前时区 date -R 2. 修改设置时区 方法(1) tzselect 方法(2) 仅限于RedHat Linux 和 CentOS timeconfig 方法(3) 适用于D ...
- ROS学习(十一)—— msg srv
一.msg 和 srv介绍 1.定义 消息(msg): msg文件就是一个描述ROS中所使用消息类型的简单文本.它们会被用来生成不同语言的源代码 服务(srv): 一个srv文件描述一项服务.它包含两 ...
- Tensorflow 相关概念
一.概述 人工智能:artificial intelligence 权重: weights 偏差:biases 图中包含输入( input).塑形( reshape). Relu 层( Relulay ...
- gitlab hook declined错误
在向gitlab提交工程的时候,出现错误提示: remote: GitLab: You are not allowed to access master!remote: error: hook dec ...
- 阿里云Redis公网连接的解决办法
https://help.aliyun.com/document_detail/43850.html ECS Windows 篇 目前云数据库 Redis 需要通过 ECS 的内网进行连接访问,如果您 ...
- How to calculate elapsed / execute time in Java
How to calculate elapsed / execute time in JavaIn Java, you can use the following ways to measure el ...
- Java – Stream has already been operated upon or closed
Java – Stream has already been operated upon or closed package com.mkyong.java8; import java.util.Ar ...