题干:
代码:
/**
* 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) {
queue<TreeNode*>que;
if(root != NULL) que.push(root);
int depth = 0;
while(!que.empty()){
depth++;
int size = que.size();
for(int i = 0; i < size; i++){
TreeNode* node = que.front();
que.pop();
if(node -> left) que.push(node -> left);
if(node -> right) que.push(node -> right);
if(!node -> left && !node -> right) return depth;//找不到左右节点就直接返回深度
}
}
return depth;
}
};