一、最大深度问题

描述:

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深度的更多相关文章

  1. LeetCode二叉树实现

    LeetCode二叉树实现 # 定义二叉树 class TreeNode: def __init__(self, x): self.val = x self.left = None self.righ ...

  2. leetcode二叉树题目总结

    leetcode二叉树题目总结 题目链接:https://leetcode-cn.com/leetbook/detail/data-structure-binary-tree/ 前序遍历(NLR) p ...

  3. 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 ...

  4. 从集合中查找最值得方法——max(),min(),nlargest(),nsmallest()

    从集合中查找最值得方法有很多,常用的方法有max(),min(),nlargest(),nsmallest()等. 一.max()和min() 1.1 入门用法 直接使用max(),min(),返回可 ...

  5. day12 max min zip 用法

    max min ,查看最大值,最小值 基础玩法 l = [1,2,3,4,5] print(max(l)) print(min(l)) 高端玩法 默认字典的取值是key的比较 age_dic={'al ...

  6. max,min,Zip函数(十一)

    zip函数,拉链,传两个有序的参数,将他们一一对应为元祖形式 max,min比较默认比较一个元素,处理的是可迭代对象,相当于for循环取出每个元素进行比较,注意:不同类型之间不可比较 #!/usr/b ...

  7. group by与avg(),max(),min(),sum()函数的关系

    数据库表: create table pay_report(     rdate varchar(8),     --日期     region_id varchar(4),    --地市      ...

  8. 关于STL库中的max min swap

    嗯...   不得不说c++中的STL库是一个神奇的东西   可以使你的代码显得更加简洁....   今天就只讲STL中的三个鬼畜:   max       min       swap   具体操作 ...

  9. SQL--合计函数(Aggregate functions):avg,count,first,last,max,min,sum

    SQL--合计函数(Aggregate functions):avg,count,first,last,max,min,sum avg() 函数 定义和用法 AVG 函数返回数值列的平均值.NULL ...

  10. 49-python基础-python3-列表-常用列表统计函数-max()-min()-sum()

    max() min() sum() 1-数字列表统计 实例: 2-字符串列表统计. 根据ASCII码大小统计字符串列表的min()和max(). 注意:sum()函数无法统计字符串列表. 实例:

随机推荐

  1. JavaScript 取数组最值的方法

    1.用Math的max,min函数 var array = [10,2,3,4,5,6,30,8,9]; Math.max.apply(null,array); Math.min.apply(null ...

  2. windows远程桌面如果超出最大连接数, 使用命令行mstsc /console登录即可

    远程桌面如果超出最大连接数, 使用命令行mstsc /console登录即可. (也可以用 mstsc /admin) 可以在运行里使用mstsc /console /v:IP:远程端口即可强制登录; ...

  3. 如何在 Linux 中找到你的 公网IP 地址

    每个网站都有一个独有的公开 IP 地址,可供任何人从任何地方访问. 互联网协议Internet Protocol(IP)不需要介绍 —— 我们每天都在使用它.即使你不直接使用它,当你在浏览器上输入 w ...

  4. Python 多线程和单线程本质应用区别

    先了解下CPU的简单运行原理: 它运行速度非常快,1s内可以运行成千上万次,一个核心可以把1s切分成成千上万个时间片段,这个核心确实同时只能运行一个任务:但是可以将多个任务交替执行,比如上一个时间片段 ...

  5. PrincetonUniversity-Coursera 算法:算法简单介绍

    Course Overview What is this course? Intermediate-level survey course. Programming and proble solvin ...

  6. atitit.ntfs ext 文件系统新特性对比

    atitit.ntfs ext 文件系统新特性对比 1. 现代文件系统应该有的特性2 1.1. 恢复Log2 1.2. 压缩2 1.3. Meta ext2 1.4. Fulltextཟsearch  ...

  7. wc 命令

    Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式: wc [选项]文件... 2.命令功能: 统计指定文件中的字节数. ...

  8. 通过google地图的webservice根据城市名称获取经纬度

    谷歌Geocoding webservice接口获取经纬度信息,由于获取地点的数量级太大,2000多条记录,从response的xml格式中取出该地点的经纬度信息.google有访问限制,如果超出25 ...

  9. 4Sum_leetCode

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  10. 【Mac】之svn上传/删除文件命令

    创建文件后,进入文件夹下: ①先checkoutsvn地址: svn checkout https://xxxx:0000/svn/CM_B2B_Document/06_Testing/B2B_Ste ...