[LeetCode 题解]: Minimum Depth of Binary Tree
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.
题意很清楚,找到二叉树中深度最短的一条路径,DFS(貌似是面试宝典上的一道题)
类似的一道题:http://www.cnblogs.com/double-win/p/3737262.html
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode *root) {
if(root==NULL) return ;
if(root->left==NULL && root->right==NULL) return ; int Leftdepth = minDepth(root->left);
int Rightdepth = minDepth(root->right); if(Leftdepth==)
return Rightdepth+;
else if(Rightdepth==)
return Leftdepth+; return min(Leftdepth,Rightdepth)+;
}
};
[LeetCode 题解]: 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 OJ Minimum Depth of Binary Tree 递归求解
题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...
- [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][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 题解]: Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- Leetcode 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- 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 111 Minimum Depth of Binary Tree(DFS)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
随机推荐
- 远程Servie通信AIDL
不可以直接通过binder了. 1.先编写一个aidl文件,里边包含我们要通信的方法.(Android studio 有直接新建AIDL选项) interface myInterface { /** ...
- Netty心跳简单Demo
前面简单地了解了一下IdleStateHandler,我们现在写一个简单的心跳demo: 1)服务器端每隔5秒检测服务器端的读超时,如果5秒没有接受到客户端的写请求,也就说服务器端5秒没有收到读事件, ...
- Mysql Windows 7 异常关闭, 2003 - Can't connect to Mysql server on 'localhost' (10061) "Unknown error")
如下: 按Win+R在窗口输入services.msc 启动mysql服务
- Linux监控和安全运维 1.9 zabbix增加客户端监控
1.在客户端安装 zabbix20与服务器相对应. yum install zabbix20-agent 2.修改配置文件 vim /etc/zabbix_agentd.conf Server=127 ...
- python算法之选择排序
选择排序 选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理如下.首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大 ...
- ArraySegment的用法
string[] myArr = { "Overred", "Medloy", "Xiaoguai", "Hare" } ...
- 数组和集合(三):Set集合的使用总结
一.概述 · 继承collection接口 · 无序(不记录添加顺序).不允许元素重复.只允许存在一个null元素 二.实现类 1. HashSet · 底层其实是包装了一个HashMap实现的 · ...
- Excel VBA 入门(零)
本教程所用系统环境: Windows 10 Excel 2013 1. 添加开发工具 打开Excel,依然找到"文件"->"选项"->"自 ...
- Linux hostname主机名配置文件与文件 /etc/hosts解析(copy来的,原作者看到了别打我)
1.关于/etc/host,主机名和IP配置文件 Hosts - The static table lookup for host name(主机名查询静态表) hosts文件是Linux系统中一个负 ...
- Ajax定时局部刷新
1.局部刷新一个地方 function refreshOnTime(){ $.ajax({ //配置 }); //7秒后重复执行该函数 setInterval('refreshOnTime', 700 ...