C语言递归之二叉树的最小深度
题目描述
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明: 叶子节点是指没有子节点的节点。
示例
输入:[3,9,20,null,null,15,7]
输出:2
题目要求
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/ int minDepth(struct TreeNode* root){ }
题解
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/ int dfs(int d,struct TreeNode* r){
if(r->left==NULL&&r->right==NULL)return d+1;
if(r->left==NULL)return dfs(d+1,r->right);
if(r->right==NULL)return dfs(d+1,r->left);
return fmin(dfs(d+1,r->left),dfs(d+1,r->right));
} int minDepth(struct TreeNode* root){
if(root==NULL)return 0;
if(root->left==NULL&&root->right==NULL)return 1;
if(root->left==NULL)return dfs(1,root->right);
if(root->right==NULL)return dfs(1,root->left);
return fmin(dfs(1,root->left),dfs(1,root->right));
}
搜索到叶子节点时返回深度
当前节点的左右子节点之一为空时递归非空子节点
当前节点的左右节点都不为空时递归两子节点取其最小值
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
C语言递归之二叉树的最小深度的更多相关文章
- Leecode刷题之旅-C语言/python-111二叉树的最小深度
/* * @lc app=leetcode.cn id=111 lang=c * * [111] 二叉树的最小深度 * * https://leetcode-cn.com/problems/minim ...
- [LeetCode] Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- lintcode : 二叉树的最小深度
题目: 二叉树的最小深度 给定一个二叉树,找出其最小深度. 二叉树的最小深度为根节点到最近叶子节点的距离. 样例 给出一棵如下的二叉树: 1 / \ 2 3 / \ 4 ...
- LeetCode OJ:Minimum Depth of Binary Tree(二叉树的最小深度)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 【LeetCode】111. 二叉树的最小深度
111. 二叉树的最小深度 知识点:二叉树,递归 题目描述 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明:叶子节点是指没有子节点的节点. 示例 输入 ...
- LeetCode 二叉树的最小深度
计算二叉树的最小深度.最小深度定义为从root到叶子节点的最小路径. public class Solution { public int run(TreeNode root) { if(root = ...
- lintcode 155 二叉树的最小深度
二叉树的最小深度 描述 笔记 数据 评测 给定一个二叉树,找出其最小深度. 二叉树的最小深度为根节点到最近叶子节点的距离. 您在真实的面试中是否遇到过这个题? Yes 哪家公司问你的这个题? Ai ...
- 【easy】111. Minimum Depth of Binary Tree求二叉树的最小深度
求二叉树的最小深度: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...
- leetcode 111. 二叉树的最小深度
题目描述: 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null, ...
随机推荐
- Open Images V4 下载自己需要的类别
OpenImages V4数据集描述1)这个v4数据集主要有两种用途:对象检测及分类,意思是说可以用这个数据集训练出对象检测模型,用于识别图像中的对象类别及位置边框.视觉关系检测,比如你用这个v4数据 ...
- symfony开发步骤简述
对symfony具体开发也有了一定时间的接触了,下面说一下开发步骤,以备自己以后查看; 1.确定表关系,字段等 2.在相应的模块下的Rescourse下config/Doctrine创建相应的.orm ...
- Wannafly挑战赛5 A珂朵莉与宇宙 前缀和+枚举平方数
Wannafly挑战赛5 A珂朵莉与宇宙 前缀和+枚举平方数 题目描述 给你一个长为n的序列a,有n*(n+1)/2个子区间,问这些子区间里面和为完全平方数的子区间个数 输入描述: 第一行一个数n 第 ...
- Solr搜索解析及查询解析器用法概述
一.简介 大多数查询都使用 了标准的Solr语法.这种语法是Solr最常见的,由默认查询解析器负责处理.Solr的默认查询解析器是Lucene查询解析器[LuceneQParserPlugin类实现] ...
- ls-remote -h -t git://github.com/adobe-webplatform/eve.git
npm WARN deprecated bfj-node4@5.3.1: Switch to the `bfj` package for fixes and new features! npm WAR ...
- 2.【Spring Cloud Alibaba】实现负载均衡-Ribbon
负载均衡的两种方式 如何实现负载均衡 目前已经实现让A总能找到B,如何实现负载均衡 负载均衡的两种方式 服务器端负载均衡 客户端负载均衡 使用Ribbo实现负载均衡 Ribbon是什么 ==Netfl ...
- Android Studio 找不到夜神模拟器的解决办法
Android Studio 找不到夜神模拟器的解决办法 1.启动夜神模拟器 2.找到你电脑上的夜神安装目录,在bin目录下打开cmd窗口,运行命令 nox_adb.exe connect 127.0 ...
- codewars--js--Human Readable Time—Math对象,parseInt()
问题描述: Write a function, which takes a non-negative integer (seconds) as input and returns the time i ...
- ELK学习002:Elasticsearch 7.x 的安装及配置
Elasticsearch 的安装与启动 1.1 下载 Elasticsearch 7.6.0 下载地址:https://www.elastic.co/cn/downloads/elasticsear ...
- 在vue中继续使用layer.js来做弹出层---切图网
layer.js是一个方便的弹出层插件,切图网专注于PSD2HTML等前端切图多年,后转向Vue开发.在vue开发过程中引入layer.js的时候遇到了麻烦.原因是layer.js不支持import导 ...