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. Cucumber 使用例子

    1. junit 配置 @RunWith(Cucumber.class) @CucumberOptions(format ={"pretty","html:target/ ...

  2. server 2012系统更改电脑密码

    在server2012系统中将鼠标指针移至任务栏右侧,在弹出的操作栏中单击“设置”选项.   在打开的设置操作界面中,鼠标单击“控制面板”选项.     在打开的控制面板选项窗口中,单击“管理工具”选 ...

  3. Linux环境下安装zookeeper

    1. 下载安装文件zookeeper-3.4.6.tar.gz 镜像地址1: http://apache.fayea.com/zookeeper/ 镜像地址2: http://mirrors.hust ...

  4. 安卓5.0宣告了ARM平台全面进入64位时代

    安卓5.0宣告了ARM平台全面进入64位时代 2014年10月份,安卓5.0正式版发布了,安卓5.0支持64位CPU,安卓5.0全面启用ART运行模式,在程序安装的时候,进行预编译,新的运行环境能够使 ...

  5. java 重写 与 重载 用法

    图例: 重写: 其实就是获取其他类 和自己类相同的方法名 来使用 重载: 其实就是创建多个相同的方法名,里面装载不同的参数 重写例子: Super关键字 重载的例子:

  6. Log4j.xml配置(rolling示例)

    Log4j.xml配置(很详细) <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4 ...

  7. Linux学习笔记 - Shell 输出命令

    1. echo 命令 echo 是基本的shell输出命令,她的语法是: echo string 我们也可以使用她来定制一些输出的格式,具体如下: 输出普通字符串 echo "it is a ...

  8. Jquery获取用户控件页面中控件的值

    $('#<%= txt_P_name.ClientID%>').val()

  9. Web页面获取用户控件页面中服务器控件的值

    用户控件页面后台: public string P_Name{get { return txt_P_name.Value; }set { txt_P_name.Value = value; }} We ...

  10. jQuery笔记——插件

    验证插件 验证插件(validate.js),是一款验证常规表单数据合法性的插件.使用它,极大的解 放了在表单上繁杂的验证过程,并且错误提示显示的完善也增加了用户体验 插件都可以在JQueryUI 的 ...