/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root) {
if(root==NULL)return 0;
int m=minDepth(root->left);
int n=minDepth(root->right);
if(root->left&&!root->right){
return 1+m;
}
if(root->right&&!root->left){
return 1+n;
}
// 所以,如果左子树为空,右子树不为空,说明最小深度是 1 + 右子树的深度。
// 反之,右子树为空,左子树不为空,最小深度是 1 + 左子树的深度。 最后如果左右子树都不为空,返回左右子树深度最小值 + 1 。
return 1+min(m,n);
}
};