每日一题ing,今天是个easy题111. Minimum Depth of Binary Tree
class Solution {
public:
int minDepth(TreeNode* root) {
if(root==nullptr)
return 0;
queue<TreeNode*> q;
q.push(root);
int depth=1;
while(!q.empty()){
int n=q.size();
for(int i=0;i<n;i++){
TreeNode* p=q.front();
q.pop();
if(p->left==nullptr&&p->right==nullptr){
return depth;
}
if(p->left){
q.push(p->left);
}
if(p->right){
q.push(p->right);
}
}
depth++;
}
return depth;
}
};