Easy-题目37:111. Minimum Depth of Binary Tree

转载 2016年05月30日 20:28:06

题目原文:
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.
题目大意:
给一个二叉树,求最小深度。
最小深度指的是沿着最短的路径从根节点走到最近的叶子节点经过的节点数。
题目分析:
(1) 空树最小深度是0;
(2) 一边子树为空的情况下,最小深度为1+非空子树最小深度;
(3) 两边均有子树的情况下,最小深度为1+左右子树最小深度的最小值。
据此很容易写出递归算法。
源码:(language:c)

int minDepth(struct TreeNode* root) {
    if(!root)
        return 0;
    else if(!root->right)
        return 1+minDepth(root->left);
    else if(!root->left)
        return 1+minDepth(root->right);
    else
        return 1+min(minDepth(root->left),minDepth(root->right));
}
int min(int a,int b) {
    return a<b?a:b;
}

成绩:
4ms,beats 37.3%,众数4ms,62.7%.
cmershen的碎碎念:
第二和第三个if不能省略,否则对一边子树为空的情况则返回1.(根节点到空树)。

111. Minimum Depth of Binary Tree [easy] (Python)

题目链接https://leetcode.com/problems/minimum-depth-of-binary-tree/题目原文 Given a binary tree, find its ...

111.Minimum Depth of Binary Tree(Tree-Easy)

111.Minimum Depth of Binary Tree(Tree-Easy) 代码:Python3 C++

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

【leetcode】111. Minimum Depth of Binary Tree

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

LeetCode 111. Minimum Depth of Binary Tree(二叉树最小深度)

原题网址:https://leetcode.com/problems/minimum-depth-of-binary-tree/ Given a binary tree, find its min...
  • jmspan
  • jmspan
  • 2016年05月24日 00:18
  • 345

Leetcode_111_Minimum Depth of Binary Tree

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41964249 Given a binary tree, ...

LeetCode111 Minimum Depth of Binary Tree

详细见:leetcode.com/problems/minimum-depth-of-binary-tree Java Solution: github package leetcod...
  • zxwtry
  • zxwtry
  • 2017年04月29日 18:56
  • 126

【LeetCode】111. Minimum Depth of Binary Tree解法及注释,Java,C++,DFS

111. Minimum Depth of Binary Tree  Total Accepted: 104964 Total Submissions: 342426 Difficulty: Ea...

104/111 Maximum/Minimum Depth of Binary Tree(二叉树求深度)

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

111. Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the s...
  • x_shuck
  • x_shuck
  • 2016年03月18日 23:14
  • 100
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Easy-题目37:111. Minimum Depth of Binary Tree
举报原因:
原因补充:

(最多只允许输入30个字)