剑指offer 面试题32 层序遍历二叉树、分行打印二叉树、之字形打印二叉树

层序遍历二叉树,使用队列
分行打印二叉树,使用队列
之字形打印二叉树,使用两个栈

#include<iostream>
#include<deque>
#include<stack>
using namespace std;


struct BinaryTreeNode 
{
	int m_nValue;
	BinaryTreeNode* m_pLeft;
	BinaryTreeNode* m_pRight;
};

//层序遍历,用到了队列
void PrintFromTopToBottom(BinaryTreeNode* pTreeRoot)
{
	if (!pTreeRoot)
		return;
	deque<BinaryTreeNode*> dequeTreeNode;
	dequeTreeNode.push_back(pTreeRoot);
	while (!dequeTreeNode.empty())
	{
		BinaryTreeNode* pNode = dequeTreeNode.front();
		dequeTreeNode.pop_front();
		cout << pNode->m_nValue << " ";
		if (pNode->m_pLeft)
			dequeTreeNode.push_back(pNode->m_pLeft);
		if (pNode->m_pRight)
			dequeTreeNode.push_back(pNode->m_pRight);

	}
}


//分行从上到下打印二叉树 

void Print(BinaryTreeNode* pRoot)
{
	if (pRoot == nullptr)
		return;
	deque<BinaryTreeNode*> nodes;
	nodes.push_back(pRoot);
	int nextLevel = 0;
	int toBePrint = 1;
	while (!nodes.empty())
	{
		BinaryTreeNode* pNode = nodes.front();
		cout << pNode->m_nValue << " ";
		if (pNode->m_pLeft != nullptr)
		{
			nodes.push_back(pNode->m_pLeft);
			++nextLevel;
		}
		if (pNode->m_pRight != nullptr)
		{
			nodes.push_back(pNode->m_pRight);
			++nextLevel;
		}
		nodes.pop_front();
		--toBePrint;
		if (toBePrint == 0)
		{
			toBePrint = nextLevel;
			nextLevel = 0;
			cout << endl;

		}

	}
}


//之字形打印二叉树

void Print2(BinaryTreeNode* pRoot)
{
	if (pRoot == nullptr)
		return;
	stack<BinaryTreeNode*> leves[2];
	int current = 0;
	int next = 1;
	leves[current].push(pRoot);
	while (!leves[0].empty() || !leves[1].empty())
	{
		BinaryTreeNode* pNode = leves[current].top();
		leves[current].pop();

		cout << pNode->m_nValue << " ";
		if (current == 0)
		{
			if (pNode->m_pLeft != nullptr)
				leves[next].push(pNode->m_pLeft);
			if (pNode->m_pRight != nullptr)
				leves[next].push(pNode->m_pRight);
		}
		else
		{
			if (pNode->m_pRight != nullptr)
				leves[next].push(pNode->m_pRight);
			if (pNode->m_pLeft != nullptr)
				leves[next].push(pNode->m_pLeft);
		}
		if (leves[current].empty())
		{
			cout << endl;
			current = 1 - current;
			next = 1 - next;
		}
	}

}

void creatTree(BinaryTreeNode* root, BinaryTreeNode* left, BinaryTreeNode* right)
{
	root->m_pLeft = left;
	root->m_pRight = right;
}
int main()
{
	/*         树
	         root1(1)
	     a(2)             b(3)
	c(4)      d(5)    e(6)    f(7)

	*/
	BinaryTreeNode* root1 = new BinaryTreeNode();
	root1->m_nValue = 1;
	root1->m_pLeft = nullptr;
	root1->m_pRight = nullptr;

	BinaryTreeNode* a = new BinaryTreeNode();
	a->m_nValue = 2;
	a->m_pLeft = nullptr;
	a->m_pRight = nullptr;

	BinaryTreeNode* b = new BinaryTreeNode();
	b->m_nValue = 3;
	b->m_pLeft = nullptr;
	b->m_pRight = nullptr;

	BinaryTreeNode* c = new BinaryTreeNode();
	c->m_nValue = 4;
	c->m_pLeft = nullptr;
	c->m_pRight = nullptr;

	BinaryTreeNode* d = new BinaryTreeNode();
	d->m_nValue = 5;
	d->m_pLeft = nullptr;
	d->m_pRight = nullptr;

	BinaryTreeNode* e = new BinaryTreeNode();
	e->m_nValue = 6;
	e->m_pLeft = nullptr;
	e->m_pRight = nullptr;

	BinaryTreeNode* f = new BinaryTreeNode();
	f->m_nValue = 7;
	f->m_pLeft = nullptr;
	f->m_pRight = nullptr;

	creatTree(root1, a, b);
	creatTree(a, c, d);
	creatTree(b, e, f);
	
	PrintFromTopToBottom(root1); // 层序遍历
	cout << endl;
	Print(root1); // 分行从上到下打印二叉树
	cout << endl;
	Print2(root1);//之字形打印二叉树


	cin.get();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值