目录

蓝桥杯备考二叉树之前中后序遍历

目录

蓝桥杯备考:二叉树之前中后序遍历

https://i-blog.csdnimg.cn/direct/65bc1813d1ef45d2b23d26113abf293b.png

https://i-blog.csdnimg.cn/direct/90cfc5d605874e29b902052c43ee9988.png

#include <iostream>
using namespace std;
const int N = 1e6+10;
int l[N],r[N];
void dfs1(int root)
{
	if(!root) return;
	cout << root << " ";
	dfs1(l[root]);
	dfs1(r[root]);
	
}
void dfs2(int root)
{
	if(!root) return;
	dfs2(l[root]);
	cout << root << " ";
	dfs2(r[root]);
}
void dfs3(int root)
{
	if(!root) return;
	dfs3(l[root]);
	dfs3(r[root]);
	cout << root << " ";
}
int main()
{
	int n;cin >> n;
	for(int i = 1;i<=n;i++)
	{
		cin >> l[i] >> r[i];
	}
	
	dfs1(1);
	cout << endl;
	dfs2(1);
	cout << endl;
	dfs3(1);
	cout << endl;
	
	
	
	
	
	return 0;
}