【LeetCode】:二叉树的Max,Min深度
一、最大深度问题
描述:
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
For example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its depth = 3.
解答:
/**
* 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 maxDepth(TreeNode* root) {
if(!root)
return 0;
return max(maxDepth(root->left),maxDepth(root->right)) + 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.
解答:
/**
* 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) {
if(!root)
return 0;
if(!root->left)
return 1 + minDepth(root->right);
if(!root->right)
return 1 + minDepth(root->left);
return min(minDepth(root->left),minDepth(root->right)) + 1;
}
};
二、java:
描述:
-------------------------------------------------
给定一个二叉树,找出其最小深度。
给出一棵如下的二叉树:
1
/ \
2 3
/ \
4 5
这个二叉树的最小深度为 2
-----------------------------------------------------------
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的距离。
给出一棵如下的二叉树:
1
/ \
2 3
/ \
4 5
这个二叉树的最大深度为3.
------------------------------------------------------------
解题思路:
此类型的题目使用遍历子树的方法,递归找到左子树和右子树,多一层计数加1,最后左子树与右子树的层数比较,选出最小或者最大深度。其中最小深度因为有0干扰,需要多个条件判断。
代码如下:
public class 二叉树的最大深度 {
/**
* Definition of TreeNode:
*/
public class TreeNode {
public int val;
public TreeNode left, right;
public TreeNode(int val) {
this.val = val;
this.left = this.right = null;
}
}
/**
* @param root: The root of binary tree.
* @return: An integer.
*/
public int maxDepth(TreeNode root) {
// write your code here
int res=0;
res=depth(res,root);
return res;
}
public int depth(int res,TreeNode root){
if(root==null)
return res;
if(root.left==null && root.right==null)
return res+1;
int res1=depth(res,root.left)+1;
int res2=depth(res,root.right)+1;
res = Math.max(res1,res2);
return res;
}
}
//*************************************
public class 二叉树的最小深度 {
/**
* @param root: The root of binary tree.
* @return: An integer.
*/
public int minDepth(TreeNode root) {
// write your code here
int res=0;
res=depth(res,root);
return res;
}
public int depth(int res,TreeNode root){
if(root==null)
return res;
if(root.left==null && root.right==null)//只有当左右子树都为空时才是叶子节点,这里不能用“||”
return res+1;
if(root.left!=null && root.right==null) //分别判断左右子树各自为空情况
return res=depth(res,root.left)+1;
if(root.left==null && root.right!=null)
return res=depth(res,root.right)+1;
int res1=depth(res,root.left)+1;
int res2=depth(res,root.right)+1;
res = Math.min(res1,res2);
return res;
}
}
【LeetCode】:二叉树的Max,Min深度的更多相关文章
- LeetCode二叉树实现
LeetCode二叉树实现 # 定义二叉树 class TreeNode: def __init__(self, x): self.val = x self.left = None self.righ ...
- leetcode二叉树题目总结
leetcode二叉树题目总结 题目链接:https://leetcode-cn.com/leetbook/detail/data-structure-binary-tree/ 前序遍历(NLR) p ...
- 6.组函数(avg(),sum(),max(),min(),count())、多行函数,分组数据(group by,求各部门的平均工资),分组过滤(having和where),sql优化
1组函数 avg(),sum(),max(),min(),count()案例: selectavg(sal),sum(sal),max(sal),min(sal),count(sal) from ...
- 从集合中查找最值得方法——max(),min(),nlargest(),nsmallest()
从集合中查找最值得方法有很多,常用的方法有max(),min(),nlargest(),nsmallest()等. 一.max()和min() 1.1 入门用法 直接使用max(),min(),返回可 ...
- day12 max min zip 用法
max min ,查看最大值,最小值 基础玩法 l = [1,2,3,4,5] print(max(l)) print(min(l)) 高端玩法 默认字典的取值是key的比较 age_dic={'al ...
- max,min,Zip函数(十一)
zip函数,拉链,传两个有序的参数,将他们一一对应为元祖形式 max,min比较默认比较一个元素,处理的是可迭代对象,相当于for循环取出每个元素进行比较,注意:不同类型之间不可比较 #!/usr/b ...
- group by与avg(),max(),min(),sum()函数的关系
数据库表: create table pay_report( rdate varchar(8), --日期 region_id varchar(4), --地市 ...
- 关于STL库中的max min swap
嗯... 不得不说c++中的STL库是一个神奇的东西 可以使你的代码显得更加简洁.... 今天就只讲STL中的三个鬼畜: max min swap 具体操作 ...
- SQL--合计函数(Aggregate functions):avg,count,first,last,max,min,sum
SQL--合计函数(Aggregate functions):avg,count,first,last,max,min,sum avg() 函数 定义和用法 AVG 函数返回数值列的平均值.NULL ...
- 49-python基础-python3-列表-常用列表统计函数-max()-min()-sum()
max() min() sum() 1-数字列表统计 实例: 2-字符串列表统计. 根据ASCII码大小统计字符串列表的min()和max(). 注意:sum()函数无法统计字符串列表. 实例:
随机推荐
- springMVC 前后台日期格式传值解决方式之一(共二) @DateTimeFormat的使用和配置
无意中发现对于时间字符串转Date类,根本不用自己去写转换类,spring mvc已经实现了该功能,还是基于注解的,轻松省事,使用 org.springframework.format.support ...
- NSNotification的几点说明
1.NSNotification消息的同步性 ①NSNotification使用的是同步操作.即如果你在程序中的A位置post了一个NSNotification,在B位置注册了一个observer,通 ...
- atititi.soa 微服务 区别 联系 优缺点.doc
atititi.soa 微服务 区别 联系 优缺点.doc 1. 应用微服务的动机,跟传统巨石应用的比较1 2. 面向服务架构(SOA) esb2 3. 微服务架构(Microservices)2 ...
- PHP 依据IP地址获取所在城市
有这种需求,须要依据用户的IP地址,定位用户所在的城市. 本文记录性文章,无逻辑性.有这样需求的朋友.能够直接拷贝使用.直接上代码,不需赘述. <? php header('Content-Ty ...
- iOS Masonry 查看更多 收起
Masonry 查看更多 收起效果实现,带动画 demo下载地址: https://github.com/qqcc1388/MasonryDemo
- Jquery 中Ajax使用的四种情况
<script type="text/javascript" language="javascript" src="JS/jquery-1[1] ...
- PyCharm搭建Spark开发环境 + 第一个pyspark程序
一, PyCharm搭建Spark开发环境 Windows7, Java 1.8.0_74, Scala 2.12.6, Spark 2.2.1, Hadoop 2.7.6 通常情况下,Spark开发 ...
- Oracle PL/SQL 高级编程
1. 复合数据类型--记录类型 Ø 语法格式 type 类型名 is record ( 字段1 字段1类型 [not null]:=表达式1; 字段2 字段2类型 [not n ...
- Dijkstra 算法——计算有权最短路径(边有权值)
[0]README 0.1) 本文总结于 数据结构与算法分析, 源代码均为原创, 旨在理解 Dijkstra 的思想并用源代码加以实现: 0.2)最短路径算法的基础知识,参见 http://blog. ...
- unity 切换场景
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; ...