minimum-depth-of-binary-tree (搜索)
题意:输出一个二叉树的最小深度。
思路:搜索一下就行了。
注意:搜索的时候,是比较每个子树的左右子树的大小,每个子树的深度要加上根节点!
class Solution {
public:
int run(TreeNode *root) {
if (root == NULL) return ; //空树
if (root->left == NULL) return run(root->right) + ;
if (root->right == NULL) return run(root->left) + ;
int left = run(root->left);
int right = run(root->right);
return (left < right) ? (left+) : (right+);
}
};
兄弟题
maximum-depth-of-binary-tree
题意:输出最大的二叉树的深度
class Solution {
public:
int maxDepth(TreeNode *root) {
if (root == NULL)return ;
if (root->left == NULL) maxDepth(root->right) + ;
if (root->right == NULL)maxDepth(root->left) + ;
int left = maxDepth(root->left) + ;
int right = maxDepth(root->right) + ;
return left > right ? left : right;
}
};
minimum-depth-of-binary-tree (搜索)的更多相关文章
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...
- 【LeetCode练习题】Minimum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- LeetCode My Solution: Minimum Depth of Binary Tree
Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...
- LeetCode: Minimum Depth of Binary Tree 解题报告
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- 【LeetCode】111. Minimum Depth of Binary Tree (2 solutions)
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- LeetCode OJ Minimum Depth of Binary Tree 递归求解
题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...
随机推荐
- 【学习笔记】AJAX内容拓展
题记——近期回顾<javaScript高级程序设计>ajax章节,发现书中对封装一个ajax库并不完美,结合之前看到博客,对ajax库做一点点的优化. 参考博客:如何去封装一个ajax库 ...
- Vim 字符串替换命令
命令模式下输入如下命令可实现替换: s/str1/str2/ 替换当前行第一个 str1 为 str2 s/str1/str2/g 替换当前行中所有的 str1 为 str2 m,ns/str1/st ...
- [转]ui-grid User can't select the row by clicking the select checkbox available in the respective row when enableFullRowSelection : true"
本文转自:https://github.com/angular-ui/ui-grid/issues/5239 Try this style to enable checkbox selection: ...
- 《Microsoft SQL Server 2012 T-SQL Fundamentals》
书名 <SQL Server 2012 T-SQL基础教程> 图片 时间 2017-8 学习 每章后面有习题很适合我,看完写sql的能力有质的飞跃好书 http://tsql.soli ...
- Mybatis generator代码生成
背景 项目中使用Mybatis做持久层框架,但由于开发成员水平不一,写dao的时候,各有各的偏好,有时候还会写出带sql注入漏洞的代码. 出现sql注入漏洞,一般是#和$的区别没弄明白: $ 直接把字 ...
- 设计模式之命令模式(Command )
命令模式是我们能够实现发送者和接收者之间的完全解耦,发送者是调用操作的对象,而接收者是接收请求并执行特定操作的对象.通过解耦,发送者无需了解接收者的接口.在这里,请求的含义是需要被执行的命令. 作用 ...
- SQL Server 创建和修改数据表
一.CREATE语句(创建) 1.创建DataBase 1.CONTAINMENT SQL Server 2012 新功能 , 默认值是OFF .(太高级 书上也没有详细介绍). 2.ON ON用于两 ...
- Python十讲 - 第一讲:从零开始学Python
之后慢慢添加... Python语言的背景知识
- 【读书笔记】iOS-成为一名开发者
iOS开发者计划是按年付费的,在过期前60天可以开始续费.如果你不续费的话,你将无法发布应用.另外苹果会吊销你的开发者证书和发布证书.最后,苹果将你在iTunes App Store上的所有应用下架. ...
- CSS布局设置
一 盒模型 盒模型 在CSS中,"box model"这一术语是用来设计和布局时使用,然后在网页中基本上都会显示一些方方正正的盒子.我们称为这种盒子叫盒模型. 盒模型有两种:标准模 ...