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 a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int dfs(TreeNode *r){
if(r==NULL) return ;
if(r->left==NULL&&r->right==NULL){
return ;
}
else{
if(r->left==NULL){
return dfs(r->right)+;
}
else if(r->right==NULL){
return dfs(r->left)+;
}
else{
return min(dfs(r->left),dfs(r->right)) + ;
}
}
} int minDepth(TreeNode* root) {
return dfs(root);
}
};

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

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

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

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

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

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

  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

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

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

  7. leetcode 111 Minimum Depth of Binary Tree ----- java

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

  8. Java [Leetcode 111]Minimum Depth of Binary Tree

    题目描述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...

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

随机推荐

  1. 阿里云数据库RDS迁移,DTS 迁移过程中,是否会锁表,对源数据库是否有影响?

    阿里云数据库RDS迁移,DTS 迁移过程中,是否会锁表,对源数据库是否有影响? DTS 在进行全量数据迁移和增量数据迁移的过程中,均不会对源端数据库进行锁表,因此在全量数据迁移和增量数据迁移的过程中, ...

  2. Java使用笔记之对象比较

    1.关于java对象的比较,经常会遇见比较某个两个对象的多个属性是否相等,可以通过重写对象equals方法来实现. 比如有两个User,如果姓名和年龄相等的话,我们就可以认为他们重复的数据.那么我们就 ...

  3. 【Android】怎样写一个JsBridge

    JsBridge 简单介绍 Android JsBridge 就是用来在 Android app的原生 java 代码与 javascript 代码中架设通信(调用)桥梁的辅助工具. 原文地址点这里 ...

  4. Linux在中国正在走向没落

    在中国,Linux正在走向没落,一片萧条景象. 在这样的大背景下.居然有人愿意接手中科红旗,令人佩服! 在中国,没有一个关于国际Linux的官方刊物(或站点)反映国际Linux运动的真实声音.Linu ...

  5. AspectJ学习笔记2-Eclipse中AspectJ插件AJDT的正确安装方法

    接着之前一篇日志. 这个事情也挺无语的.简单记录一下. 在这里:http://www.eclipse.org/ajdt/ 能够下载最新的Eclipse Plugin.下载解压之后,一般来说.直接把解压 ...

  6. 启动Eclipse时,启不起来JVM terminated. Exit code=-1

    启动Eclipse时,启不起来JVM terminated. Exit code=-1 出现错误了,不知道什么原因原本好好的Eclipse,今天早上出问题了,启动不起来还抛出JVM terminate ...

  7. LeetCode108_Convert SortedArray to BinarySearchTree(将有序数组转成二叉排序树) Java题解

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  8. PowerBuilder -- 调试(Debug)

    @.进入代码调试,执行下一步直接就退出了调试 原文:http://bbs.csdn.net/topics/390126005 处理方法:尝试把 Watch 中查看的变量全部删掉.

  9. HTTP POST请求数据提交格式(转)

    FROM: http://bbs.125.la/thread-13743350-1-1.html HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PUT ...

  10. 知识复习(LDT+TSS+GATE+INTERRUPT)

    [1]README 1.0)由于实现进程的切换任务,其功能涉及到 LDT + TSS +GATE + INTERRUPT:下面我们对这些内容进行复习: 1.1) source code from or ...