文章

59

粉丝

0

获赞

331

访问

8.6k

头像
二叉排序树 - 华科 题解:map+deliver father=AC
P1396 华中科技大学
发布于2026年3月12日 00:44
阅读数 49

#include <iostream>
#include <map>
using namespace std;

map<int,int> father;//store node's father
typedef struct BTNode {
	int value;
	struct BTNode* lc, * rc;
}BTNode, * BST;
void InsertTree(BST& T,int temp, int x)
{
	if (T == NULL)
	{
		T = new BTNode;
		T->value = x;
		father[x]=temp;
		T->lc = NULL;
		T->rc = NULL;
		return;
	}
	if (T->value == x)
		return;
	else if (T->value > x)
		InsertTree(T->lc,T->value, x);
	else
		InsertTree(T->rc,T->value, x);
}
void preorder(BST T, string& s)
{
	if (T)
	{
		s += T->value;
		preorder(T->lc, s);
		preorder(T->rc, s);
	}
}
void DeleteTree(BST& T)
{
	if (T)
	{
		DeleteTree(T->lc);
		DeleteTree(T->rc);
		delete T;
	}
}
int main()
{
	int n;
	while (cin >> n)
	{
		BST T = NULL;
		for (int i = 0; i < n; i++)
		{
			int x;
			cin>>x;
			InsertTree(T,-1, x);
			cout<<father[x]<<endl;
		}...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发