文章

74

粉丝

0

获赞

98

访问

9.0k

头像
题解思路:插入分支过程判断是否存在孩子节点,若不存在则可确定为父节点
P1396 华中科技大学
发布于2025年8月14日 19:30
阅读数 130

#include<bits/stdc++.h>
using namespace std;

typedef struct node{
	int data;
	struct node *lchild, *rchild;
}*BitTree;

void Insert(BitTree &T, int x){
	if(T == NULL){
		T = new node;
		T->data = x;
		T->lchild = NULL;
		T->rchild = NULL;
		return;
	}
	if(x < T->data){
		if(T->lchild == NULL) cout << T->data << endl;
		Insert(T->lchild, x);
	}
	else if(x > T->data){
		if(T->rchild == NULL) cout << T->data << endl;
		Insert(T->rchild, x);
	}
}

int main(){
	
	int n;
	while(cin >> n){
		BitTree root = NULL;
		cout << "-1" << endl;
		for(int i = 0; i < n; i++){
			int x; cin >> x;
			Insert(root, x);
		}
	}
	
	return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发