【第22期】观点:IT 行业加班,到底有没有价值?

[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);
}
版权声明: 举报

相关文章推荐

LeetCode111 Minimum Depth of Binary Tree

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

Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth. <p style="margin-bottom: 10px; color: #333333; font-fami

程序员升职加薪指南!还缺一个“证”!

CSDN出品,立即查看!

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

Minimum Depth of Binary Tree @LeetCode

/** * Minimum Depth of Binary Tree * * Given a binary tree, find its minimum depth. * * The minimum depth is the number of nodes along the shor...

Leetcode 111. Minimum Depth of Binary Tree

Minimum Depth of Binary Tree
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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