文章

37

粉丝

1

获赞

294

访问

5.4k

头像
二叉搜索树 - 复旦2020 题解:
P996 复旦大学2020年机试题
发布于2026年2月28日 18:53
阅读数 199

只能过90%

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

typedef struct node{
	int data;
	node* left;
	node* right;
	node* father;
}*BiTree;

BiTree build(BiTree root,int x,BiTree father){
	if(root == nullptr){
		BiTree t = new node;
		t->left = nullptr;
		t->right = nullptr;
		t->data = x;
		t->father = father;
		return t;
	}
	
	if(root->data > x){
		root->left = build(root->left,x,root);
	}
	else{
		root->right = build(root->right,x,root);
	}

	return root;
}

void InOrder(BiTree root){
	if(root == nullptr)
		return ;
	
	InOrder(root->left);
	
	if(root->father == nullptr)
		cout<<0<<' ';
	else
		cout<<root->father->data<<' ';
	
	InOrder(root->right);
}


int main(){
	int n;
	cin>>n;
	vector<int> v;
	for(int i = 0 ; i < n; i++){
		int t; cin>>t;
		v.push_back(t);
	}
	BiTree root = nullptr;
	for(int i = 0 ; i < v.size(); i++){
		r...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发