版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tangyuanzong/article/details/80144097
https://leetcode.com/problems/minimum-depth-of-binary-tree/description/
题目:求二叉树的最小深度
思路:直接用BFS
class Solution {
public:
int minDepth(TreeNode* root) {
int re = 1;
if(!root) return 0;
TreeNode *q[10000];
int l=0,r=1;
int num = 1,next_num = 0;
q[l] = root;
while(l<r){
TreeNode *temp = q[l];
num--;
if(!temp->left&&!temp->right) return re;
if(temp->left) q[r] = temp->left,r++,next_num++;
if(temp->right) q[r] = temp->right,r++,next_num++;
if(num==0){
num = next_num;
next_num = 0;
re++;
}
l++;
}
return re;
}
};