题目:
比如 二叉树 1
2 3
4 5 6 7 输出 1 2 3 4 5 6 7
思路:
将根结点1 压入队列,弹出1的同时将其左右子节点2 3压入队列, 要弹出2时, 将其子节点压入队列 , 以此类推
代码:
typedef struct BinaryTreeNode
{
int val_;
struct BinaryTreeNode *pLeft;
struct BinaryTreeNode *pRight;
}Node;
void Func(Node *pNode)
{
if (!pNode)
return;
queue<Node *> que;
que.push(pNode);
while (!que.empty())
{
Node *pTemp = que.front();
cout << pTemp->val_ << " ";
if (pTemp->pLeft)
que.push(pTemp->pLeft);
if (pTemp->pRight)
que.push(pTemp->pRight);
que.pop();
}
}