题目:从上往下打印出二叉树的每一个结点,同一层的结点从左到右顺序打印。
按层遍历.
/二叉树结点的定义:
struct BinaryTreeNode
{
int m_value;
BinaryTreeNode* m_left;
BinaryTreeNode* m_right;
};
int Print(BinaryTreeNode * head)
{
queue<BinaryTreeNode *>inque;//队列用来存放子节点
if(head==NULL)//二叉树为空
return -1;
//if(head->m_left==NULL&&head->m_right==NULL)//只有一个结点
// return head->m_value;
inque.push(head);
while(inque.size())
{
BinaryTreeNode *pNode=inque.front();
inque.pop(); //pop 处理后使得队列里面每次只有一个二叉树结点
printf("%d",pNode->m_value);//打印根结点
if(pNode->m_left) //操作左子树
inque.push(pNode->m_left);
if(pNode->m_right) //操作左子树
inque.push(pNode->m_right);
}
}