社区讨论

二叉排序树插入问题求助

灌水区参与者 4已保存回复 8

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
8 条
当前快照
1 份
快照标识符
@mi7wkpr3
此快照首次捕获于
2025/11/21 04:47
4 个月前
此快照最后确认于
2025/11/21 04:47
4 个月前
查看原帖
CPP
#include<cstdio>
#include<iostream>
int n,a;
struct Node{
	int data;
	Node *lson,*rson;
}*root;
void Insert(int num,Node *p)
{
	if(p==NULL)
	{
		p=new Node;
		p->data=num;
	}
	else
		num<p->data?Insert(num,p->lson):Insert(num,p->rson);
}
void dfs(Node *p)
{
	if(p!=NULL)
	{
		dfs(p->lson);
		printf("%d\n",p->data);
		dfs(p->rson);
	}
}
int main(int argc, char const *argv[])
{
	freopen("test.txt","r",stdin);
	scanf("%d",&n);
	for(int i=1;i<=n;++i)
		scanf("%d",&a),Insert(a,root);
	dfs(root);
	return 0;
}
为什么我的根节点硬是没有指向一个空间?

回复

8 条回复,欢迎继续交流。

正在加载回复...