二叉树三种遍历方式

根据给定的前序遍历和中序遍历序列,利用相同元素作为分割点,递归地构造二叉树。

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

#include <iostream>
#include <stack>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;

#define ITE

struct TreeNode
{
	int val;
	TreeNode* left;
	TreeNode* right;
	TreeNode(int v) : val(v), left(NULL), right(NULL) {}
};

#ifdef REC
void preorder(TreeNode* root)
{
	if (!root) return;
	cout << root->val << " ";
	preorder(root->left);
	preorder(root->right);
}

void inorder(TreeNode* root)
{
	if (!root) return;
	inorder(root->left);
	cout << root->val << " ";
	inorder(root->right);
}

void postorder(TreeNode* root)
{
	if (!root) return;
	postorder(root->left);
	postorder(root->right);
	cout << root->val << " ";
}

#else if (define ITE)
void preorder(TreeNode* root)
{
	if (!root) return;
	stack<TreeNode*> sta;
	sta.push(root);
	while (!sta.empty())
	{
		TreeNode* temp = sta.top();
		sta.pop();
		cout << temp->val << " ";
		if (temp->right) sta.push(temp->right);
		if (temp->left)  sta.push(temp->left);
	}
}

void inorder(TreeNode* root)
{
	if (!root) return;
	stack<TreeNode*> sta;
	set<TreeNode*> visited;
	sta.push(root);
	while (!sta.empty())
	{
		TreeNode* temp = sta.top();
		if (visited.find(temp) == visited.end())
		{
			visited.insert(temp);
			if (temp->left) sta.push(temp->left);
		}
		else
		{
			sta.pop();
			cout << temp->val << " ";
			if (temp->right) sta.push(temp->right);
		}
	}
}

void postorder(TreeNode* root)
{
	if (!root) return;
	vector<int> vec;
	stack<TreeNode*> sta;
	sta.push(root);
	while (!sta.empty())
	{
		TreeNode* temp = sta.top();
		sta.pop();
		vec.push_back(temp->val);
		if (temp->left) sta.push(temp->left);
		if (temp->right) sta.push(temp->right);
	}
	reverse(vec.begin(), vec.end());
	for (int i = 0; i < vec.size(); ++i)
		cout << vec[i] << " ";
}
#endif

int main()
{
	TreeNode* ptn1 = new TreeNode(1);
	TreeNode* ptn2 = new TreeNode(2);
	TreeNode* ptn3 = new TreeNode(3);
	TreeNode* ptn4 = new TreeNode(4);
	TreeNode* ptn5 = new TreeNode(5);
	TreeNode* ptn6 = new TreeNode(6);
	ptn1->left = ptn2;
	ptn1->right = ptn3;
	ptn2->left = ptn4;
	ptn2->right = ptn5;
	ptn3->right = ptn6;

	preorder(ptn1);
	cout << endl;

	inorder(ptn1);
	cout << endl;

	postorder(ptn1);
	cout << endl;

	system("pause");
	return 0;
}

另:二叉树构造

问题:给出前序遍历和中序遍历,生成二叉树(本题的前提是序列中每个数都不同)

方法:通过查找两个序列中相同的元素,将序列分割,递归构造

struct TreeNode
{
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

TreeNode* construct(vector<int> &pre, int i, int j, vector<int> &in, int m, int n);

TreeNode* reconstructBT(vector<int> &pre, vector<int> &in)
{
	if (pre.empty() || in.empty() || pre.size() != in.size()) return NULL;
	return construct(pre, 0, pre.size() - 1, in, 0, in.size() - 1);
}

TreeNode* construct(vector<int> &pre, int i, int j, vector<int> &in, int m, int n)
{
	if (i > j || m > n) return NULL;
	int k = m;
	while (k <= n && in[k] != pre[i]) ++k;
	TreeNode* node = new TreeNode(in[k]);
	node->left = construct(pre, i + 1, i + k - m, in, m, k - 1);
	node->right = construct(pre, i + k - m + 1, j, in, k + 1, n);
	return node;
}




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值