二叉树的各种递归和栈的遍历。

本文详细介绍了二叉树的多种遍历方法,包括递归和非递归方式实现的前序、中序、后序及层次遍历,并提供完整的C++代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<stack>
#include<queue>
using namespace std;
struct node{
	int data;
	node*left;
	node*right;
	node(int x) :data(x), left(nullptr), right(nullptr){}
};
//新建节点
node*newNode(int elem)
{
	node*cur = new node(elem);
	cur->left = nullptr;
	cur->right = nullptr;
	return cur;
}
//递归插入节点
node*InsertNode(node*head, int elem)
{
	if (head == nullptr)return newNode(elem);
	else if (elem < head->data)
		head->left = InsertNode(head->left, elem);
	else
		head->right = InsertNode(head->right, elem);
	return head;
}
//Recursion
void pre_order(node*head)
{
	if (head == nullptr)return;
	cout << head->data << " ";
	pre_order(head->left);
	pre_order(head->right);
}
void in_order(node*head)
{
	if (head == nullptr)return;
	in_order(head->left);
	cout << head->data << " ";
	in_order(head->right);
}
void post_order(node*head)
{
	if (head == nullptr)return;
	post_order(head->left);
	post_order(head->right);
	cout << head->data << " ";
}
//Stack
void pre_order_stack(node*head)
{
	if (head == nullptr)return;
	node*cur = head;
	stack<node*>st;
	st.push(cur);
	while (!st.empty())
	{
		node*tmp = st.top();
		st.pop();
		cout << tmp->data << " ";
		if (tmp->right)
			st.push(tmp->right);
		if (tmp->left)
			st.push(tmp->left);
	}

}
void in_order_stack(node*head)
{
	if (head == nullptr)return;
	node*cur = head;
	stack<node*>st;
	while (!st.empty() || cur != nullptr)
	{
		if (cur != nullptr)
		{
			st.push(cur);
			cur = cur->left;
		}
		else
		{
			cur = st.top();
			st.pop();
			cout << cur->data << " ";
			cur = cur->right;
		}
	}
}
void post_order_stack(node*head)
{
	node*cur = head;
	node*tag;
	stack<node*>st;

	do
	{
		while (cur != nullptr)
		{
			st.push(cur);
			cur = cur->left;
		}
		tag = nullptr;
		while (!st.empty())
		{
			cur = st.top();
			st.pop();
			//有孩子不存在或已被访问,访问之
			if (cur->right == tag)
			{
				cout << cur->data << " ";
				tag = cur;
			}
			else
			{
				//重新进栈
				st.push(cur);
				cur = cur->right;
				break;
			}
		}
	} while (!st.empty());
}
void level_order_stack(node*head)
{
	if (head == nullptr)return;
	queue<node*>myq;
	node*cur = head;
	myq.push(cur);
	while (!myq.empty())
	{
		node*tmp = myq.front();
		myq.pop();
		cout << tmp->data << " ";
		if (tmp->left)
			myq.push(tmp->left);
		if (tmp->right)
			myq.push(tmp->right);
	}
}
int main()
{
	int A[8] = { 5, 3, 8, 1, 4, 7, 9, 6 };
	node*head = nullptr;
	for (int i = 0; i < 8; i++)
		head = InsertNode(head, A[i]);
	cout << "pre_order\n";
	pre_order(head);
	cout << endl;
	pre_order_stack(head);
	cout << endl;

	cout << "in_order\n";
	in_order(head);
	cout << endl;
	in_order_stack(head);
	cout << endl;

	cout << "post_order\n";
	post_order(head);
	cout << endl;
	post_order_stack(head);
	cout << endl;

	cout << "level_order\n";
	level_order_stack(head);
	return 0;
}

二叉树遍历也可以不用栈,Morris 方法。

                                         点击这里

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值