文章

19

粉丝

0

获赞

168

访问

4.7k

头像
二叉排序树 - 华科 题解:
P1396 华中科技大学
发布于2026年3月1日 17:56
阅读数 162

#include <cstdio>
#include <iostream>
#include <vector>
#include <string>
#include <map>

using namespace std;

struct TNode
{
	int data;
	TNode *l,*r;
};

void create(TNode*&t,int val,int father)
{
	if(t==nullptr)
	{
		t=new TNode;
		t->l=nullptr;
		t->r=nullptr;
		t->data=val;
		cout<<father<<endl;
	}
	else
	{
		if(t->data>val)
		{
			create(t->l,val,t->data);
		}
		else
		{
			create(t->r,val,t->data);
		}
	}
}

int main()
{
	int n;
	while(cin>>n)
	{
		int num;
		TNode *t=nullptr;
		for(int i=0;i<n;i++)
		{
			cin>>num;
			create(t,num,-1);
		}
	}
	
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发