`
xitonga
  • 浏览: 585850 次
文章分类
社区版块
存档分类
最新评论

二叉树层序遍历

 
阅读更多

二叉树的创建,销毁与先序,中序,后序遍历参见

http://blog.csdn.net/walkerkalr/article/details/20287119

以下是利用队列来层序遍历的算法:

void levelOrder(BinaryTreeNode* pRoot)
{
	if(pRoot != NULL)
	{
		queue<BinaryTreeNode*> btnQueue;
		btnQueue.push(pRoot);
		while(!btnQueue.empty())
		{
			pRoot = btnQueue.front();
			printf("%d\t",pRoot->m_nValue);
			btnQueue.pop();
			if(pRoot->m_pLeft)
				btnQueue.push(pRoot->m_pLeft);
			if(pRoot->m_pRight)
				btnQueue.push(pRoot->m_pRight);
		}
	}
}

==转载请注明出处,谢谢!
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics