关闭

LeetCode:Minimum Depth of Binary Tree

标签: LeetCodeMinimum Depth of Bin
267人阅读 评论(0) 收藏 举报
分类:

Minimum Depth of Binary Tree

Total Accepted: 81059 Total Submissions: 274649 Difficulty: Easy

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.

Subscribe to see which companies asked this question












code:

/**
 * 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 0;
        int l = minDepth(root->left);
        int r = minDepth(root->right);
        
        if(0==l) return r+1;
        else if(0==r) return l+1;
        else return min(l,r) + 1;
    }
};


0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】

【111-Minimum Depth of Binary Tree(二叉树的最小深度)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a binary tre...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-11 07:37
  • 2686

【算法编程】求二叉树的深度-Maximu Depth of Binary Tree

题目大意:很简单,只需要找出一颗二叉树的最大深度即可,貌似没有时间和空间的要求。求解方法:更简单,只需要按照宽度优先的方法去查找即可,在这里我用a队列保存待扩展的节点,用b来保存a扩展出来的节点,再利...
  • chenriwei2
  • chenriwei2
  • 2015-07-03 18:18
  • 1426

二叉搜索树(Binary Search Tree )的定义及分析

定义:     二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 每个结点都有一个作为搜索依据的关键码(key),所有结点的关键码互不相同。 左子树(如果非空)上所...
  • jsh13417
  • jsh13417
  • 2013-01-22 21:59
  • 3322

【LeetCode】Minimum Depth of Binary Tree

题目 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th...
  • Mars_NAVY
  • Mars_NAVY
  • 2014-12-20 15:36
  • 891

minimum-depth-of-binary-tree--《LeetCode》

题目描述: Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the...
  • u011514810
  • u011514810
  • 2017-06-29 15:03
  • 98

[LeetCode][Java] Minimum Depth of Binary Tree

题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes al...
  • Evan123mg
  • Evan123mg
  • 2015-07-21 09:56
  • 548

【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】

【111-Minimum Depth of Binary Tree(二叉树的最小深度)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a binary tre...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-11 07:37
  • 2686

[LeetCode]Minimum Depth of Binary Tree

解题思路: 宽度搜索。 边界条件:root 为NULL 前条件:queue,root->val  = 1(深度),push(root) 不变式:只要queue不空,取front,把它的左右子树pu...
  • aFeiOnePiece
  • aFeiOnePiece
  • 2015-07-22 09:48
  • 278

leetcode-111-Minimum Depth of Binary Tree

Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
  • u014705854
  • u014705854
  • 2015-07-23 22:04
  • 383

LeetCode-Minimum Depth of Binary Tree

作者:disappearedgod 文章出处:http://blog.csdn.net/disappearedgod/article/details/23621903 时间:2014-6-7 ...
  • disappearedgod
  • disappearedgod
  • 2014-06-07 12:49
  • 996
    个人资料
    • 访问:377416次
    • 积分:6659
    • 等级:
    • 排名:第4143名
    • 原创:318篇
    • 转载:1篇
    • 译文:0篇
    • 评论:33条
    博客专栏
    最新评论