LeetCode 112 Minimum Depth of Binary Tree

Posted Shendu.cc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 112 Minimum Depth of Binary Tree相关的知识,希望对你有一定的参考价值。

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],


return its minimum depth = 2.

 

求最小的深度,二叉树,这道题目我认为数据是有问题的,所以有176个踩,

c++

class Solution {
public:
    int minDepth(TreeNode* root) {
        if(root==NULL) return 0;
        int lmin =0;
        int rmin =0;
        if(root->left!=NULL)
         lmin = minDepth(root->left);
        if(root->right!=NULL)
         rmin = minDepth(root->right);
        if(lmin==0)
            return rmin+1;
        if(rmin==0)
            return lmin+1;
        return min(lmin,rmin)+1;
        
        
        
        
    }
};

 

以上是关于LeetCode 112 Minimum Depth of Binary Tree的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode Minimum Height Trees

leetcode:Minimum Subarray

leetcode@ [310] Minimum Height Trees

LeetCode112路径总和

LeetCode -- Minimum Path Sum

LeetCode 2187. Minimum Time to Complete Trips