Leetcode 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.

注意题目到达的时叶子节点

class Solution {
public:
    int minDepth(TreeNode *root) {
        if(!root) return 0;
        else if(!root->left && !root->right) return 1;
        else if(root->left && !root->right) return 1+minDepth(root->left);
        else if(!root->left && root->right) return 1+minDepth(root->right);
        else return 1+min(minDepth(root->left), minDepth(root->right));
    }
};
原文地址:https://www.cnblogs.com/xiongqiangcs/p/3810960.html