最小深度和最大深度不太一样
C++ 代码
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root) {
if(!root){
return 0;
}
auto left=minDepth( root->left);
auto right=minDepth( root->right);
if(left==0||right==0){
return left+right+1;
}
return min(left,right)+1;
}
};