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.

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) return ; min_depth = INT_MAX;
dfs(root,);
return min_depth; }
void dfs(TreeNode* node, int depth){
if(node->left)
{
dfs(node->left,depth+);
} if(node->right)
{
dfs(node->right,depth+);
} if(!node->left && !node->right)
{
if(depth < min_depth)
{
min_depth = depth;
}
}
}
private:
int min_depth;
};

111. Minimum Depth of Binary Tree (Tree; DFS)的更多相关文章

  1. [LeetCode] 111. Minimum Depth of Binary Tree_Easy tag:DFS

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  2. 【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 ...

  3. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  4. (二叉树 BFS DFS) 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 ...

  5. 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 ...

  6. leetcode 111 minimum depth of binary tree

    problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...

  7. [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 ...

  8. 力扣算法题—111.Minimum Depth of Binary Tree

      Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the sh ...

  9. 【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

随机推荐

  1. Django ORM哪些操作

    一般操作 看专业的官网文档,做专业的程序员! 必知必会13条 <1> all(): 查询所有结果 <2> filter(**kwargs): 它包含了与所给筛选条件相匹配的对象 ...

  2. 关于CSS单位:rem vh vw vmin vmax

    rem(root em) 如果你给body设置了font-size字体大小,那么body的任何子元素的1em就是等于body设置的font-size demo: body {  font-size: ...

  3. 选择排序算法-python实现

    #-*- coding: UTF-8 -*- import numpy as np def SelectSort(a): for i in xrange(0,a.size): min = a[i] p ...

  4. linux命令ls -l的默认排序方式

    差不多快实现完了ls -l,但是在测试阶段发现一个问题,对于包含[a-ZA-Z]之外的字符,系统的排序方式并不一样. 很想了会儿,总算发现原来它的排序方式是无视[a-ZA-Z]之外的字符的 至于怎么发 ...

  5. 螺旋填数:读入两个整数m,n,输出一个m行n列的矩阵,这个矩阵是1~m*n这些自然数按照右、下、左、上螺旋填入的结果。

    package Day8_06; /*读入两个整数m,n,输出一个m行n列的矩阵,这个矩阵是1~m*n这些自然数按照右.下.左.上螺旋填入的结果. * 例如读入数字4,5,则输出结果为: * 1 2 ...

  6. virtual Box centos7 公司网络环境下不能联网的解决方案

    首先感谢@采蘑菇的东峰的博客 的分享 原文:http://blog.sina.com.cn/s/blog_8d92d7580102vhky.html ------------------------- ...

  7. Cassandra Wiki Login JmxSecurity

    JmxSecurity 监控和管理Cassandra

  8. python做语音信号处理

    音频信号的读写.播放及录音 标准的python已经支持WAV格式的书写,而实时的声音输入输出需要安装pyAudio(http://people.csail.mit.edu/hubert/pyaudio ...

  9. 十六.jQuery源码解析之Sizzle设计思路.htm

    为了便于后面的叙述,需要了解一些相关术语和约定. 并列选择器表达式:"div,p,a"====>div,p,a是并列的. 块表达式:"div>p"中 ...

  10. microtip Tooltip工具提示样式

    最近开发项目,想增加滑动提示文字,类似img alt和i的title,但是效果都不是很理想,当然jq也有,但是用起来比较繁琐,使用不是特别方便 于是在github上看到了一个不错的库: https:/ ...