[leetcode-111]Minimum Depth of Binary Tree(C语言)

原创 2015年07月08日 20:55:11

问题描述:
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;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
int findDepth(struct TreeNode* root,int step){
    if(root->left==NULL&&root->right == NULL)
        return step+1;
    int leftStep = 0;
    int rightStep = 0;
    if(root->left)
        leftStep = findDepth(root->left,step+1);
    if(root->right)
        rightStep = findDepth(root->right,step+1);

    if(leftStep == 0)
        return rightStep;
    else if(rightStep == 0)
        return leftStep;
    else
        return (leftStep<rightStep)?leftStep:rightStep;
}
int minDepth(struct TreeNode* root) {
    if(!root)
        return 0;

    return findDepth(root,0);
}

相关文章推荐

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
  • 336

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
  • 118

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

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

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

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

[leetcode]111.Minimum Depth of Binary Tree

111. Minimum Depth of Binary TreeGiven a binary tree, find its minimum depth. The minimum depth is ...

[leetcode] 111. Minimum Depth of Binary Tree 解题报告

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

LeetCode 111.Minimum Depth of Binary Tree

题目大意是求一颗树的最小深度。从根节点到叶子节点每往下一层,深度加1,根节点的深度为1。       先贴代码:       int m = 9999; int depth = 1; void d...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[leetcode-111]Minimum Depth of Binary Tree(C语言)
举报原因:
原因补充:

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