一、最大深度问题

描述:

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. 键盘上所有键位的ascii值

    上次用的时候一直找,这次找到了,收藏起来. 0x1 鼠标左键 0x2 鼠标右键 0x3 CANCEL 键 0x4 鼠标中键 0x8 BACKSPACE 键 0x9 TAB 键 0xC CLEAR 键 ...

  2. appium for mac 安装与测试ios说明

    一.安装 安装dmg,可以自己下载appium-1.4.0.dmg或者找rtx我要,文件过大不能添加附件. Appium提供了一个doctor,运行appium-doctor 如果有问题,Fix it ...

  3. ubuntu 14.04 anaconda安装

    Python的准备工作 Python 一个备受欢迎的点是社区支持很多,有非常多优秀的库或者模块.但是某些库之间有时候也存在依赖,所以要安装这些库也是挺繁琐的过程.但总有人忍受不了这种 繁琐,都会开发出 ...

  4. 搭建Squid反向代理服务器

    好吧,更新个文章,有段时间没写技术博文了.今天就说说squid反向代理这个服务,当然,这是在Linux下配置完成的.说自己没偏见似乎不可能 了.大概是相对喜欢Linux而已.但我从不否认Windows ...

  5. Hibernate二次学习一----------Hibernate简单搭建

    因为博客园自带的markdown不太好用,因此所有markdown笔记都使用cmd_markdown发布 Hibernate二次学习一----------Hibernate简单搭建: https:// ...

  6. MySQL优化时可以设置的几个参数

    back_log:back_log值指出在MySQL暂时停止回答新请求之前的短时间内多少个请求可以被存在堆栈中.也就是说,如果MySql的连接数据达到max_connections时,新来的请求将会被 ...

  7. Android加入新的视频格式--媒体库扫描

    需求:在mediaprovider数据库中加入.mov后缀格式的视频文件 能够使用工具MediaInfo_GUI_0.7.67_Windows.3243836749.exe 查看mov文件编码格式类型 ...

  8. oracle中建同名

    create synonym TD_B_REDIS_COUNT for ucr_param.TD_B_REDIS_COUNT;grant DELETE,UPDATE,INSERT,SELECT on ...

  9. redhat安装中文man手册

    1.下载中文man手册 http://download.chinaunix.net/download.php?id=13232&ResourceID=6537 2.上传至服务器并解压 tar ...

  10. iOS系列译文:自定义Collection View布局

    原文出处: Ole Begemann   译文出处: 黄爱武(@answer-huang).欢迎加入技术翻译小组. UICollectionView在iOS6中第一次被介绍,也是UIKit视图类中的一 ...