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)的更多相关文章

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

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

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

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

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

  5. Leetcode 111 Minimum Depth of Binary Tree 二叉树

    找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...

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

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

  8. 111 Minimum Depth of Binary Tree 二叉树的最小深度

    给定一个二叉树,找出其最小深度.最小深度是从根节点到最近叶节点的最短路径的节点数量.详见:https://leetcode.com/problems/minimum-depth-of-binary-t ...

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

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

随机推荐

  1. hihocoder offer收割编程练习赛11 A hiho字符串

    思路: 我用的尺取. 注意题目描述为恰好2个'h',1个'i',1个'o'. 实现: #include <iostream> #include <cstdio> #includ ...

  2. 将call/apply方法应用于其他对象上的几种方法

    在处理类数组中,发现了两种将数组方法应用于类数组的方法,现将call/apply的常用方式总结一下. 一.当做函数调用 function print_vars(var1,var2){ console. ...

  3. kubernetesV1.13.1一键部署脚本(k8s自动部署脚本)

    部署k8sv1.13.1只需要下面几步就OK了: git clone https://github.com/luckman666/deploy_Kubernetes-v1.13.1.git cd de ...

  4. git --删除文件、重命名

    修改最后一次提交 git commit --amend -m “” 删除文件:. git rm <需要删除的文件> 只是删除当前工作目录和暂存区的文件,也就是取消跟踪.在下次提交时不纳入版 ...

  5. LigerUI用Post\Get\Ajax前后台交互方式的写法

    parms 参数统一 json格式的数据 url 访问后台的url 设置同步参数 [javascript] view plain copy   $.ajaxSetup({ async : false} ...

  6. 触发器deleted 表和 inserted 表详解

    摘要:触发器语句中使用了两种特殊的表:deleted 表和 inserted 表. create trigger updateDeleteTimeon userfor updateasbegin  u ...

  7. swift 与 @objc

    Objective-C entry points https://github.com/apple/swift-evolution/blob/master/proposals/0160-objc-in ...

  8. createuser - 定义一个新的 PostgreSQL 用户帐户

    SYNOPSIS createuser [ option...] [ username] DESCRIPTION 描述 createuser 创建一个新的 PostgreSQL 用户.只有超级用户(在 ...

  9. java中属性命名get字母大小写问题

    java文件 company.java private int sTime; public void setSTime (int sTime) {        this.sTime = sTime; ...

  10. 为什么 [\000-\177]匹配任意7bit ascii码 ?

    如题 41     print \000;     42     print "\n";     43     print \177; 输出: SCALAR(0x3fce0)SCA ...