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 ...
随机推荐
- Django之视图层
一.视图函数 一个视图函数,简称视图,是一个简单的python函数,接收web请求并返回web响应.响应可以是一张网页的HTML内容,一个重定向,一个404错误等.在函数中必须写一个request的参 ...
- js------数组随机排序和去重
let arr = ['g', 'b', 'c', 'd', 'e', 'a', 'g', 'b', 'c']; // 数组随机排序(原数组被修改)Array.prototype.randomSort ...
- 细说Redis(一)之 Redis的数据结构与应用场景
这一篇文章主要介绍Redis的数据结构与应用场景 NOSQL之Redis Redis是一款由key-value存储的软件.说起NOSQL,有文档型.键值型.列型存储.图形数据库.其中,在简单的读写性能 ...
- .net 服务端 访问共享文件夹
共享文件夹所在电脑为A服务器,网站部署在B服务器 A,B服务器上拥有同名账户,且密码也要相同.如账户名share,密码123. A服务器上,共享文件夹设置share账户有读写权限 B服务器上,IIS中 ...
- JNI和NDK基础
引言 JNI是Java Native Interface(Java本地接口),是为了方便Java调用C和C++等本地代码所封装的一层接口. NDK是Android提供的一个工具集合,通过NDK可以在A ...
- Java String的简单介绍
一.String类的构造方法(先粗略介绍三种 分别是s1,s2,s3) 二.String的常用判断方法 三.String类的常用获取方法 三.Sting的常用转换方法 四.String其他功能 五 ...
- ORM作业
使用之前学到过的操作实现下面的查询操作:1. 查询平均成绩大于60分的同学的id和平均成绩:2. 查询所有同学的id.姓名.选课的数量.总成绩; 3. 查询姓“李”的老师的个数: 4. 查询没学过“李 ...
- ArcGIS Arcade
ArcGIS Arcade http://127.0.0.1/Resource/JavaScript/arcgis_js_api/sdk/4.10/latest/guide/arcade/index. ...
- system.transfer.list版本进化
从android5.0开始之后,recovery升级包中不再升级system.img,而是升级system.new.dat+system.transfer.list的这种文件组合,经过android版 ...
- vue权威指南笔记02——对比v-if与v-show
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...