[leetcode] Minimum Depth of Binary Tree

Minimum Depth of Binary Tree


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


你可能感兴趣的:([leetcode] Minimum Depth of Binary Tree)