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 ...
随机推荐
- 提交应用 Windows Phone的应用程序认证要求
本文介绍了 Windows Phone 应用程序或游戏要通过认证并在 Windows Phone Marketplace 中发布而必须满足的策略和技术要求. 1.0 计划概述 设计认证过程的一个核心原 ...
- WIN7 x64下java 8的环境变量配置
Oracle官网下载JDK进行安装:我下载的是Java 8 JDK,地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-do ...
- InChatter系统之客户端消息处理中心
一.模块结构 首先来看下客户端消息处理中心模块的简单结构: ChatCallback:服务器端我们定义的回调接口IChatCallback的客户端实现 ChatMsgCenter:服务端的消息处理中心 ...
- taskctl的后台字符界面登录不了解决办法
今天在使用taskctl的designer时,十多分钟挂了2次,每次挂了之后就签不出来了,只能等半小时,然后在taskctl的QQ群里咨询了,给的解决方案是 http://www.taskctl.co ...
- ubuntu 下安装redis
获取Redis 1.通过官网http://redis.io/获取稳定版源码包下载地址: 2.通过wget http://download.redis.io/releases/redis-3.0.2.t ...
- 集成新版(5.17+)Activiti Modeler与Rest服务
声明: 此教程适合Activiti 5.17+版本. 本博客所涉及的内容均可在kft-activiti-demo中找到. 在线demo可以访问 http://demo.kafeitu.me:8080/ ...
- 模拟Java-Sping,实现其IOC和AOP核心
简易的IOC框图 注解的方式实现了左边的分支,那么就剩下右边的XML分支: XmlContext:这个类是也是AbstractApplicationContext的子类,和AnnotationCont ...
- C# 支持多线程
C# 支持多线程并行执行程序 .一个程序由一个单线程开始,该单线程由CLR和操作系统创建而成,并具有多线程创建额外线程的功能. .创建线程的方法 2.1 通过Thread类来创建线程. ThreadS ...
- 使用cmd查看Tensorboard的生成图
代码中"graph_practice7/"文件存放的是生成的神经网络图 现在要查看这个图,使用cmd 进入文件指定目录(我的文件存储地址是E:\workspace\PycharmP ...
- HTML head meta标签详细
<!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --> <html lang="zh-cmn-Hans"&g ...