利用队列
每次先入队列根
遍历栈顶(根)
出队列
然后依次入队列左右孩子
只要队列不为空,循环上述步骤
struct Node
{
T _data;
Node* _pLeft;
Node* _pRight;
};
void LevelOrder()
{
queue<T> q;
Node* treenode;
if (treenode==NULL)
{
return ;
}
q.push(treenode);
while (!q.empty())
{
Node* cur=q.front();
cout<<cur->_data<<" ";
q.pop();
if (cur->_pLeft)
{
q.push(cur->_pLeft);
}
if (cur->_pRight)
{
q.push(cur->_pRight);
}
}
}