Minimum Depth of Binary Tree

来源:互联网 发布:postman post传递json 编辑:程序博客网 时间:2024/06/03 20:10
class Solution {
public:
int minDepth(TreeNode* root) {
int res=0;
bool flag = false;
queue<TreeNode*> q;
TreeNode *t;
if (root == NULL)
return 0;
q.push(root);
q.push(NULL);
while (q.front() != NULL) {
res++;
while (q.front() != NULL) {
t = q.front();
q.pop();
if (t->left == NULL&&t->right == NULL) {
flag = true;
break;
}
if (t->left != NULL)
q.push(t->left);
if (t->right != NULL)
q.push(t->right);
}
if (flag)
break;
q.pop();
q.push(NULL);
}
return res;
}
};