您的位置:首页 > 其它

Minimum Depth of Binary Tree

2015-07-09 11:09 274 查看
https://leetcode.com/problems/minimum-depth-of-binary-tree/



/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root) {
queue<TreeNode *> q1;
if(root==NULL)
return 0;
q1.push(root);
int depth=0;
bool flag=0;
while(!q1.empty())
{
depth++;
queue<TreeNode *> q2;
while(!q1.empty())
{
TreeNode * temp=q1.front();
q1.pop();
if(temp->left==NULL&&temp->right==NULL)
{
flag=1;
break;
}
if(temp->left!=NULL)
q2.push(temp->left);
if(temp->right!=NULL)
q2.push(temp->right);
}
q1=q2;
if(flag==1)
break;
}
return depth;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: