104:

class Solution {
public:
int maxDepth(TreeNode* root) {
if(root == NULL)
return ;
int left = maxDepth(root->left);
int right = maxDepth(root->right);
return (left > right ? left : right) + ;
}
};

111:

class Solution {
public:
int minDepth(TreeNode* root) {
if(root == NULL)
return ;
int left = minDepth(root->left);
int right = minDepth(root->right);
if(left == )
return right + ;
else if(right == )
return left + ;
else
return left < right ? left + : right + ;
}
};

最小的深度这个题与最大的深度这个题稍稍有点不同,因为最小深度的计算必须从叶子节点开始,没有叶子节点不能计算,所以1,2这种情况只能返回2,不能返回1。做个判断即可。

leetcode 104. Maximum Depth of Binary Tree 111. Minimum Depth of Binary Tree的更多相关文章

  1. [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  2. [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 long ...

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

  4. LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告

    104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...

  5. LeetCode 104. Maximum Depth of Binary Tree

    Problem: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along ...

  6. (二叉树 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 long ...

  7. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  8. [LeetCode]题解(python):111 Minimum Depth of Binary Tree

    题目来源 https://leetcode.com/problems/minimum-depth-of-binary-tree/ Given a binary tree, find its minim ...

  9. LeetCode 111. Minimum Depth of Binary Tree (二叉树最小的深度)

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

随机推荐

  1. mysql 8小时超时设置

    1.打开MySQL配置文件 2.添加 interactive_timeout=31536000wait_timeout=31536000 3.重新启动服务 打开MySQL命令行界面查看设置是否成功

  2. HDU6191(01字典树启发式合并)

    Query on A Tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Othe ...

  3. Spider-one

    1. 爬虫是如何采集网页数据的: 网页的三大特征: -1. 每个网页都有自己的 URL(统一资源定位符)地址来进行网络定位. -2. 每个网页都使用 HTML(超文本标记语言)来描述页面信息. -3. ...

  4. api接口签名认证的一种方式

    请求方 try { using (var client = new HttpClient()) { StringContent content = new StringContent(strParam ...

  5. JavaScript大杂烩7 - 理解内置集合

    JavaScript内置了很多对象,简单的类型如String,Number,Boolean (相应的"值类型"拥有相同的方法),复杂一点的如Function,Object,Arra ...

  6. [20180408]那些函数索引适合字段的查询.txt

    [20180408]那些函数索引适合字段的查询.txt --//一般不主张建立函数索引,往往是开发的无知,使用trunc等函数,实际上一些函数也可以用于字段的查询.--//以前零碎的写过一些,放假看了 ...

  7. 手动将经典 VM 从 VHD 迁移到新的 ARM 托管磁盘 VM

    本部分有助于将现有 Azure VM 从经典部署模型迁移到资源管理器部署模型中的托管磁盘. 计划迁移到托管磁盘 本部分可帮助你针对 VM 和磁盘类型做出最佳决策. 位置 选取 Azure 托管磁盘可用 ...

  8. 洗礼灵魂,修炼python(12)--python关键词,包

    关键词 1.什么是关键词: 就是系统已经定义好的一些关键词语法,可以直接使用,很明显的就是在IDE里这些关键词会高亮显示的就是 2.有哪些关键词: 前面我们一直在说关键词,内置函数,到底有哪些关键词对 ...

  9. AMP架构补充与wordpress部署

    1.httpd的虚拟主机不能使用的问题 httpd中新建一个虚拟主机,并添加访问URI路径的时候,需要给此路径指定访问权限.今天遇到一个虚拟主机不能使用的问题,语法检测没有报错,并且还可以正常启动服务 ...

  10. Vs .Net Framework 灵活配置

    背景:我们开发和部署项目时都是通过注释某些配置项 比如: 在调试时就注释掉生产的配置项,在生产时又要改回来,只有一个还好,如果多的话就会非常容易出错. 问题1:在发布时容易出错,需要控制发布时根据配置 ...