leetcode_question_111 Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

int minDepth(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(root == NULL) return 0;
queue<TreeNode*> que;
que.push(root);
int count = 1;
int depth = 0;
while(!que.empty())
{
TreeNode* tmp = que.front();
que.pop();
count--; if(tmp->left == NULL && tmp->right == NULL) break; if(tmp->left)
que.push(tmp->left);
if(tmp->right)
que.push(tmp->right); if(count == 0){
depth++;
count = que.size();
}
}
return ++depth;
}
上一篇:打包成zip


下一篇:101210-450789-147200(可以激活Xshell5,而且可以升级) 亲测可用 只能用于xshell5