111. Minimum Depth of Binary Tree(Tree)
阿新 • • 發佈:2019-01-01
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;
}
};