二叉树的层序遍历

 

关于层序遍历的STL代码如下,注意reserve的妙用,否则会因为vector容量增长导致跌代器失效:

 

下面代码不知为何总报错,伤感了,眼看找工作高峰来临,时间紧迫,扔这了。。。

void printLevel(Node *root)
{
	if(root == NULL)
		return;

	vector<Node *> nvec;
	nvec.reserve(100);
	nvec.push_back(root);
	vector<Node *>::iterator startIter = nvec.begin();
	vector<Node *>::iterator lastIter = nvec.end();

	while(startIter != lastIter) {
		while(startIter != lastIter) {
			cout << (*startIter)->data << " ";
			Node *left = (*startIter)->left;
			Node *right = (*startIter)->right;
			if(left)
				nvec.push_back(left);
			if(right)
				nvec.push_back(right);
			++startIter;
		}
		cout << endl;
		lastIter = nvec.end();
	}
}


 再次写二叉树的层序遍历,现在的代码可以说是简洁高效,窝稀饭

void printLevel(Node *root)
{
	if (root == NULL)
		return;

	vector<Node *> nvec;
	nvec.push_back(root);
	int start = 0;
	int end = 1;
	int pos = end;

	while (start != end)
	{
		Node *node = nvec[start];
		cout << node->item << " ";
		start++;

		if (node->left)
		{
			nvec.push_back(node->left);
			end++;
		}
		if (node->right)
		{
			nvec.push_back(node->right);
			end++;
		}

		if (start == pos)
		{
			pos = end;
			cout << endl;
		}
	}
}

关于拓展问题:

访问顺序变为:

78

456

23

1

访问顺序变为:

87

654

32

1

用栈很容易实现~

void putNode2Stack(Node *root, stack<Node *> &snode)
{
	if (root == NULL)
		return;

	vector<Node *> nvec;
	nvec.push_back(root);
	snode.push(root);
	snode.push(NULL);
	int start = 0;
	int end = 1;
	int pos = end;

	while (start != end)
	{
		Node *node = nvec[start];
		cout << node->item << " ";
		start++;

		if (node->left)
		{
			nvec.push_back(node->left);
			snode.push(node->left);
			end++;
		}
		if (node->right)
		{
			nvec.push_back(node->right);
			snode.push(node->right);
			end++;
		}

		if (start == pos)
		{
			pos = end;
			snode.push(NULL);
			cout << endl;
		}
	}
}

void putNode2Stack2(Node *root, stack<Node *> &snode)
{
	if (root == NULL)
		return;

	vector<Node *> nvec;
	nvec.push_back(root);
	snode.push(root);
	snode.push(NULL);
	int start = 0;
	int end = 1;
	int pos = end;

	while (start != end)
	{
		Node *node = nvec[start];
		cout << node->item << " ";
		start++;

		if (node->right)
		{
			nvec.push_back(node->right);
			snode.push(node->right);
			end++;
		}
		if (node->left)
		{
			nvec.push_back(node->left);
			snode.push(node->left);
			end++;
		}

		if (start == pos)
		{
			pos = end;
			snode.push(NULL);
			cout << endl;
		}
	}
}

利用queue来处理层序遍历

void printLevel(Node *root)
{
	if (root == NULL)
		return;

	queue<Node *> nqueue;
	nqueue.push(root);

	int start = 0;
	int last = 1;
	int tempLast = last;
	while (!nqueue.empty())
	{
		Node *top = nqueue.front();
		nqueue.pop();
		cout << top->data << " ";

		if (top->left)
		{
			nqueue.push(top->left);
			last++;
		}
		if (top->right)
		{
			nqueue.push(top->right);
			last++;
		}

		start++;
		if (start == tempLast)
		{
			tempLast = last;
			cout << endl;
		}
	}
}

关于层序遍历,可以出现一个变种题目,那就是NULL指针打印成空格

void printLevel(Node *root)
{
	if (root == NULL)
		return;

	queue<Node *> qnode;
	qnode.push(root);
	int count = 1;
	int start = 0;
	int tempCount = 0;

	while (!qnode.empty())
	{
		tempCount = 0;
		while (start != count)
		{
			Node *top = qnode.front();
			
			if (top != NULL)
			{
				if (top->left)
				{
					qnode.push(top->left);
					tempCount++;
				}
				else if (top->left == NULL && top->right != NULL)
				{
					qnode.push(NULL);
					tempCount++;
				}
				if (top->right)
				{
					qnode.push(top->right);
					tempCount++;
				}
				else if (top->left != NULL && top->right == NULL)
				{
					qnode.push(NULL);
					tempCount++;
				}
			}

			start++;
			if (top != NULL)
				cout << top->data;
			else
				cout << " ";
			qnode.pop(); 
		}
		count = tempCount;
		start = 0;
		cout << endl;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值