树的先序、中序、后序和层次遍历-C++实现----面试题目-----同时也可以说是树的层次历遍

本文详细介绍了树的四种遍历方法:先序遍历(递归与非递归)、中序遍历(递归与非递归)、后序遍历(递归与非递归)以及层次遍历的两种方法,是面试中的常见题目,也是理解树结构的重要途径。

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

一、先序遍历
1、递归算法

				struct Tree
				{
					int date;
					Tree* lchild;
					Tree* rchild;
					Tree(int x):date(x),lchild(nullptr),rchild(nullptr){}
				};
				
				void PreOrder(Tree* root)
				{
					if (root != nullptr)
					{
						cout << root->date << " ";
						PreOrder(root->lchild);
						PreOrder(root->rchild);
					}
				}

2、非递归算法

					void PreOrder(Tree* root)
					{
						stack<Tree* > Stack;
						if (root == nullptr)
							return;
						while (root != nullptr || !Stack.empty())
						{
							while (root != nullptr)
							{
								Stack.push(root);
								cout << root->date << " ";
								root = root->lchild;
							}
							root = Stack.top();
							Stack.pop();
							root = root->rchild;
						}
					}

二、中序遍历
1、递归算法

					void InOrder(Tree* root)
					{
						if (root != nullptr)
						{
							InOrder(root->lchild);
							cout << root->date << " ";
							InOrder(root->rchild);
						}
					}

2、非递归算法

						void InOrder(Tree* root)
						{
							stack<Tree*> s;
							while (root != nullptr || !s.empty())
							{
								if (root != nullptr)
								{
									s.push(root);
									root = root->lchild;
								}
								else
								{
									root = s.top();s.pop();
									cout << root->date << " ";
									root = root->rchild;
								}
							}
						}

三、后序遍历
1、递归算法

					void PostOrder(Tree* root)
					{
						if (root != nullptr)
						{
							PostOrder(root->lchild);
							PostOrder(root->rchild);
							cout << root->date << " ";
						}
					}

2、非递归算法

						void PostOrder(Tree* root)
						{
							stack<Tree*> s;
							Tree* r = nullptr;//使用辅助指针,指向最近访问过的节点
							while (root != nullptr || !s.empty())
							{
								if (root != nullptr)
								{
									s.push(root);
									root = root->lchild;
								}
								else
								{
									root = s.top();
									if (root->rchild != nullptr && root->rchild != r)
										root = root->rchild;
									else
									{
										s.pop();
										cout << root->date << " ";
										r = root;
										root = nullptr;
									}
								}
							}
						}

四、层次遍历

1、方法1

					void LevelOrder(Tree* root)
					{
						if (root == nullptr)
							return;
						queue<Tree*> que;
						que.push(root);
						while (!que.empty())
						{
							root = que.front();
							cout << root->date << " ";
							que.pop();
							if (root->lchild != nullptr)
								que.push(root->lchild);
							if (root->rchild != nullptr)
								que.push(root->rchild);
						}
					}

2、方法2

						void LevelOrder(Tree* root)
						{
							if (root == nullptr)
								return;
							queue<Tree*> que;
							que.push(root);
							while (!que.empty())
							{
								int size = que.size();
								while (size)
								{
									root = que.front();
									cout << root->date << " ";
									que.pop();
									if (root->lchild != nullptr)
										que.push(root->lchild);
									if (root->rchild != nullptr)
										que.push(root->rchild);
									--size;
								}
							}
						}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

N1314N

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值