Minimum Depth of Binary Tree(二叉树DFS)
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
代码:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
int mindepth;
public:
void tra(TreeNode* root,int depth){
if(root==NULL) return;
++depth;
if(!root->left&&!root->right&&depth!=&&depth<mindepth)//必须是叶节点才更新mindepth
mindepth=depth;
tra(root->left,depth);
tra(root->right,depth);
}
int minDepth(TreeNode *root) {
mindepth=;
if(!root) return ;
if(root->left==NULL&&root->right==NULL) return ;
tra(root,);
if(mindepth==){
return ;
}
return mindepth;
}
};
Minimum Depth of Binary Tree(二叉树DFS)的更多相关文章
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- [LeetCode] 111. 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】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- [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 ...
- Leetcode 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- [Leetcode] The 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] Minimum Depth of Binary Tree 二叉树最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 111 Minimum Depth of Binary Tree 二叉树的最小深度
给定一个二叉树,找出其最小深度.最小深度是从根节点到最近叶节点的最短路径的节点数量.详见:https://leetcode.com/problems/minimum-depth-of-binary-t ...
- 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 ...
- [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
随机推荐
- CentOS7搭建LAMP
阿里云CentOS7.3搭建 Apache+MySQL+PHP环境 参考https://www.cnblogs.com/apro-abra/p/4862285.html 一.安装Apache 1. ...
- Android源码之陌陌源码
本源码是一个老版本的陌陌源码,翻了翻代码,发现有完整的登陆注册功能(基于本地)其余都是静态页面.有需要的朋友可以拿去研究一下.其中登陆账号是86930007密码为123456. 这个项目源码我也上 ...
- IE主页被恶意修改处理办法
HKEY_USERS/.DEFAULT/Software/Policies/Microsoft/Internet Explorer/Control Panel 下的DWORD值“homepage”的键 ...
- parsley.js正确使用姿势
1.第一式 当然要先引用:parsley.js 2.第二式 页面中定义需要使用自定义校验,注意红色的地方,必须要使用小写,重要的问题说三遍,小写,小写 <form class="for ...
- C# 如何发送Http请求
HttpSender是一个用于发送Http消息的轻量C#库,使用非常简单,只需要一两行代码,就能完成Http请求的发送 使用 Nuget,搜索 HttpSender 就能找到这个库 这个库的命名空间是 ...
- JavaSE-18 常用工具类
学习要点 Object类 枚举 包装类 Math类 Random类 字符串处理 日期时间 Object类 1 什么是Object类 Object类存储在java.lang包中,是所有java类(Ob ...
- NPOI--------------.Net操作Excel初步使用(导出)
背景 因公司项目需要添加数据导出功能故此添加,找了几种方式发现该方式具有 无需依赖本机安装office环境,使用灵活等优点故采用此方式. 安装 Nuget 直接安装NPOI即可 使用方式 1.根据需要 ...
- Elasticsearch入门和基本使用
1. 什么是Elasticsearch? Elasticsearch,分布式,高性能,高可用,可伸缩的搜索和分析系统:Elastic 是 Lucene 的封装,提供了 REST API 的操作接口,开 ...
- rem怎么算
* rem : html根标签的 font-size决定* em 相对于父标签的font-size 决定的* 设计稿的宽一般都是 750px ---> 20px* 750px 1rem = 10 ...
- java配置日志总结
log4j 搭建日志环境 简单非maven项目,只需要引入log4j.jar,在类路径下添加log4j.properties即可 简单maven项目,只需要在pom.xml引入dependency,在 ...