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 ...
随机推荐
- 基于Asp.Net Core 2.1的简单问答社区系统源代码分享
看见园子里很多人都在分享源代码,我也来凑个热闹. 该项目基于.NET CORE 2.1(其实是从1.1开始开发的),经历过不停的调整终于有个能拿出手的版本了,第一次在博客园发文章. 使用到的技术以及框 ...
- [HNOI2018] 道路
Description 给一棵二叉树,每个叶子节点 \(i\) 有三个属性 \(a_i,b_i,c_i\) 每个非叶子节点都能标记向左右儿子中的一条边(记作 \(x\) 边和 \(y\) 边) 设叶子 ...
- InnoDB存储引擎概览
InnoDB存储引擎概览 InnoDB存储引擎以其平衡了高可靠性和高性能性而闻名遐迩,在MySQL 8.0版本中,InnoDB存储引擎是默认的存储引擎.(历史追溯从MySQL 5.5.5版本开始, ...
- 用JDOM解析XML文件时如何解决中文问题?如何解析?
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import ja ...
- c# 虚拟路径转换为绝对路径
/// <summary> /// 解析相对Url /// </summary> /// <param name="relativeUrl">相 ...
- Mysql存储过程入门介绍
delimiter //一般情况下MYSQL以:结尾表示确认输入并执行语句,但在存储过程中:不是表示结束,因此可以用该命令将:号改为//表示确认输入并执行. 一.创建存储过程 1.基本语法: crea ...
- Java程序设计概述
摘要:1996年Java第一次发布就引起了人们的广大关注.本文简要地介绍一下Java语言的发展历史. 一.Java程序设计平台 Java是一种优秀的程序设计语言.一旦一种语言应用于某个领域,与现存代码 ...
- Java集合之HashSet源码分析
概述 HashSet是基于HashMap来实现的, 底层采用HashMap的key来保存数据, 借此实现元素不重复, 因此HashSet的实现比较简单, 基本上的都是直接调用底层HashMap的相关方 ...
- Linux常用基本命令:三剑客命令之-awk内置变量与自定义变量
AWK中,变量分为两种:内置变量与自定义变量. 常见的内置变量有: FS:输入字段分隔符, 默认为空白字符 OFS:输出字段分隔符, 默认为空白字符 RS:输入记录分隔符(输入换行符), 指定输入时的 ...
- linux学习笔记-grub模式引导进入系统
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 1.设置系统启动盘所在位置 set root='hd0,msdosx' 2.手动加载内核驱动程序并配置root目录位置 linu ...