LeetCode【111. 二叉树的最小深度】】的更多相关文章

111. 二叉树的最小深度 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回它的最小深度 2. class Solution { public int minDepth(TreeNode root) { if (root == null) { return 0; } // null节点不参与比较 if…
题目描述: 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7返回它的最小深度  2. 思路分析: 一开始的思路是直接求二叉树的最小深度,后来发现题目限定了必须是到叶结点的路径长度最短.所以需要判断当前结点是否是叶结点.判断若左子树为空,则返回右子树的到叶子结点的最短路径:否则返回左子树到叶结点的最短路径.…
问题 给出一棵二叉树,找出它的最小深度. 最小深度是指从根节点沿着最短路径下降到最近的叶子节点所经过的节点数. 初始思路 不难看出又是一个需要层次遍历二叉树的题目,只要在112基础上作出简单修改即可得出答案. class Solution { public: int minDepth(TreeNode *root) { if(!root) { ; } treeLevel_[].clear(); treeLevel_[].clear(); depth_ = ; bool flag = false;…
题目描述: 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 思路一: 把每一层的结点加入到队列,每一层i+1,到下一层时,把上一层在队列中的结点都弹出,按从左到右把下一层的结点逐个加入,如果首次遇到一个结点没有左子结点与右子节点,则返回i class Solution(object): def minDepth1(self, root): """ 按层次遍历,寻找第一个叶节点(左孩和右孩都为空) """…
使用深度优先搜索:时间复杂度O(n),空间复杂度O(logn) /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int minDepth(TreeNode…
111. 二叉树的最小深度 知识点:二叉树,递归 题目描述 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明:叶子节点是指没有子节点的节点. 示例 输入:root = [3,9,20,null,null,15,7] 输出:2 输入:root = [2,null,3,null,4,null,5,null,6] 输出:5 解法一:递归法 函数功能:求一颗二叉树的最小深度 1.终止条件:root == null, return 0: 2.该做什么:此题和…
最小深度,看起来很简单,就是左右节点的深度最小值 定义一个函数,计算其深度 class Solution { public int minDepth(TreeNode root) { if(root == null) { return 0; } else if(root.left == null && root.right ==null) { return 1; } else { int left = Depth(root.left); int right = Depth(root.rig…
题目: 给定一个二叉树,找出其最小深度. 注意最小深度的定义! 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 一.递归法 时间复杂度:O(n).需要遍历每一个节点. 空间复杂度:最差情况下,当一棵树是非平衡树的时候,例如每个节点都只有一个孩子,树的高度为n,会产生n次递归调用,因此栈的空间开销是O(N).但在最好情况下,树的高度只有log(n),栈的空间开销是O(log(N)). /** * Definition for a binary tr…
/* * @lc app=leetcode.cn id=111 lang=c * * [111] 二叉树的最小深度 * * https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/description/ * * algorithms * Easy (37.27%) * Total Accepted: 12.2K * Total Submissions: 32.6K * Testcase Example: '[3,9,20,nu…
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. 二叉树的经典问题之最小深度问题就是就最短路径的节点个数,还是用深度优先搜索DFS来完成,万能的递归啊...请看代码: /** * Definition for binary tre…
计算二叉树的最小深度.最小深度定义为从root到叶子节点的最小路径. public class Solution { public int run(TreeNode root) { if(root == null) return 0; if(root.left == null)return run(root.right) + 1; if(root.right == null) return run(root.left) + 1; return Math.min(run(root.left), r…
求二叉树的最小深度: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int minDepth(TreeNode* root) { ; int l = m…
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. 求二叉树的最小深度,额,简单的递归而已,代码如下: class Solution { public: int minDepth(TreeNode* root) { ; int le…
题目: 二叉树的最小深度 给定一个二叉树,找出其最小深度. 二叉树的最小深度为根节点到最近叶子节点的距离. 样例 给出一棵如下的二叉树: 1 /     \ 2       3 /    \ 4      5 这个二叉树的最小深度为 2 解题: 递归求解 Java程序: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public…
二叉树的最小深度   描述 笔记 数据 评测 给定一个二叉树,找出其最小深度. 二叉树的最小深度为根节点到最近叶子节点的距离. 您在真实的面试中是否遇到过这个题? Yes 哪家公司问你的这个题? Airbnb Amazon LinkedIn Cryptic Studios Dropbox Epic Systems TinyCo Hedvig Microsoft Yahoo Bloomberg Uber Snapchat Twitter Yelp Apple Google Facebook Zen…
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要在左右子树中取较大的就行了. 然而最小深度时,如果左右子树中有一个为空会返回0,这时我们是不能算做有效深度的. 所以分成了三种情况,左子树为空,右子树为空,左右子树都不为空.当然,如果左右子树都为空的话,就会返回1. 广度优先搜索(类似层序遍历的思想) 递归解法本质是深度优先搜索,但因为我们是求最小…
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. 给一个二叉树,找出它的最小深度.最小深度是从根节点向下到最近的叶节点的最短路径,就是最短路径的节点个数. 解法1:DFS 解法2: BFS Java: DFS, Time Comp…
Easy! 题目描述: 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回它的最小深度  2. 解题思路: 二叉树的经典问题之最小深度问题就是就最短路径的节点个数,还是用深度优先搜索DFS来完成,万能的递归啊...请看代码. C++解法一: /** * Definition for binary tre…
问题描述 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回它的最小深度 2. 解题思路 刚开始想得很简单,不就是类比求树的深度,空树返回0,非空树返回左右子树最小深度+1就ok吗? 如果你这么想,那真的too young too simple,sometimes naive. 因为很明显这种思路忽略了…
给定一个二叉树,找出其最小深度.最小深度是从根节点到最近叶节点的最短路径的节点数量.详见:https://leetcode.com/problems/minimum-depth-of-binary-tree/description/ Java实现: 递归实现: /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * Tree…
[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. 递归和非递归,此提比较简单.广度优先遍历即可.关键之处就在于如何保持访问深度. 下面是4种代码: im…
要求:此题正好和Maximum Depth of Binary Tree一题是相反的,即寻找二叉树的最小的深度值:从根节点到最近的叶子节点的距离. 结题思路:和找最大距离不同之处在于:找最小距离要注意(l<r)? l+1:r+1的区别应用,因为可能存在左右子树为空的情况,此时值就为0,但显然值是不为0的(只有当二叉树为空才为0),所以,在这里注意一下即可! 代码如下: struct TreeNode { int val; TreeNode *left; TreeNode *right; Tree…
给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回它的最小深度  2. #include /** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct T…
①题目 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], 返回它的最小深度  2. ②思路 使用深度优先搜索 ③代码 class Solution { public int minDepth(TreeNode root) { if (root == null) { return 0; } if ((root.left == null) &&…
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. Note: A leaf is a node with no children. Example: Given binary tree [3,9,20,null,null,15,7…
题目描述 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例 输入:[3,9,20,null,null,15,7] 输出:2 题目要求 /** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ int minD…
给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回它的最小深度  2. /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * T…
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. 方法一: 层次遍历.主体的思想不变,程序的主体结构也不变,具体遍历过程参照二叉树的层次遍历.关键在于终止条件,在某一层中,节点遍历的顺序是从左往右的,若是最短路径出现在该层中,则其…
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,找出他的最小的深度. 最小的深度,指的是从根节点到叶子节点的,经历的最小的节点个数. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Given a binary tree, find its min…
解法一:递归 遇到叶子节点不递归,否则接着往子树递归,每次递归层数加1 要确定的是,一定要保证初始输入的节点是有子节点的.因为可能出现只有单子树的情况,所以要先确认这种情况. 具体过程: 1.分析初始条件 空指针:深度为0 单节点:深度为1 1.先确定递归基本条件: 节点指针为空,说明深度为0,返回深度0: 如果到了叶节点,说明其左右两节点指针就是空,也就是在深度为0的基础上加上该节点所在的一层,即1+调用空指针的递归函数 2.找最近的叶节点,也就是左右指针都是空的叶节点的时候. /** * D…