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.

Note: A leaf is a node with no children.

Example:

Given binary tree [3,9,20,null,null,15,7],

    3
/ \
9 20
/ \
15 7

return its minimum depth = 2.

------------------------------------------------------------------------------------------------------------------------------

求二叉树的最小深度,嗯,这个题可以用DFS,也可以用BFS,我这个题先用BFS写,然后用DFS。

要注意,注意,这个最小深度指的是,从根节点到最近的叶子节点的距离,比如[1,1]这个最小深度就是2!!!,同样,最大深度也是2。

[1,1]图:

所以在用BFS时,只有这个结点的左子节点和右子节点同时为NULL时,才能确定为叶子节点,才能返回sum(表示最小深度)。

同理,在用DFS时,当左子节点为NULL,而右子节点不为空,那就不是叶子节点,还得计算。同理,如果右子节点为NULL,而左子节点也不是,就得继续递归下去。

C++代码:

/**
* 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 minDepth(TreeNode* root) {
queue<TreeNode*> q;
if(!root)
return ;
q.push(root);
int sum = ;
while(!q.empty()){
sum++;
for(int i = q.size(); i > ; i--){ //必须写循环,如果不写,对于[1,2,3,4,5],将会返回3,与题目要求不符。
auto t = q.front();
q.pop();
if(!t->left &&!t->right) return sum;
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
}
return ;
}
};

DFS:

C++代码:

/**
* 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 minDepth(TreeNode* root) {
if(!root) return ;
if(!root->left) return + minDepth(root->right); //这个要注意。
if(!root->right) return + minDepth(root->left); //这个要注意。
return min( + minDepth(root->left), + minDepth(root->right));
}
};

(二叉树 BFS DFS) leetcode 111. Minimum Depth of Binary Tree的更多相关文章

  1. (二叉树 BFS DFS) leetcode 104. Maximum Depth of Binary Tree

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

  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] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

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

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

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

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

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

  7. leetcode 111 minimum depth of binary tree

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

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

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

随机推荐

  1. INotifyPropertyChanged

    在WPF MVVM模式开发中,实现INotifyPropertyChanged的ViewModel是非常重要且常见的类: public class MainViewModel : INotifyPro ...

  2. Microsoft Azure Tutorial: Build your first movie inventory web app with just a few lines of code

    Editor’s Note: The following is a guest post from Mustafa Mahmutović, a Microsoft Student Partner wh ...

  3. 给dom对象添加事件

  4. iOS应用的性能调试

    1.Static Analysis 使用之前先清理一下数据:product-->Clean 可能遇到的问题: a.发现工程中有多个“User-facing text should use loc ...

  5. bzoj2152-[国家集训队]聪聪可可

    Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)--遇到这种问题,一般情况下石头剪刀布就好 ...

  6. shiro注解和标签

    Controller中注解: @RequiresAuthentication @RequiresGuest @RequiresPermissions("account:create" ...

  7. python之旅第八篇--异常

    判断类与对象关系 isinstance #判断对象obj是否是由cls类创建的 class Foo(object): pass obj = Foo() print isinstance(obj,Foo ...

  8. P1280 尼克的任务 dp

    思路: 倒着DP  f[i]表示i时刻的空闲时间最大值 在当前时间没有任务开始 f[i]=f[i+1]+1;    上一分钟最大空闲时间+1 在当前时间有任务开始  f[i]=max(f[i],f[i ...

  9. 【题解】 bzoj3555: [Ctsc2014]企鹅QQ (字符串Hash)

    题面戳我 Solution 我们分析题意,他要求的是两个字符串只有一个字符不同,然后我们再看长度\(L \leq 200\),显然我们就可以把每一位删除后\(Hash\),然后判断相同个数即可 我一开 ...

  10. 【BZOJ5336】[TJOI2018]party(动态规划)

    [BZOJ5336][TJOI2018]party(动态规划) 题面 BZOJ 洛谷 题解 这题好神仙啊... 考虑普通的\(LCS\)的\(dp\),\(f[i][j]=\max\{f[i-1][j ...