文章

74

粉丝

0

获赞

98

访问

8.9k

头像
二叉搜索树 题解:
P1317 浙江大学机试题
发布于2025年8月14日 19:10
阅读数 116

#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(T->data > x) Insert(T->lchild, x);
	else if(T->data < x) Insert(T->rchild, x);
}

void Pre(BitTree t, vector<int> &v){
	if(t == NULL) return;
	else {
		v.push_back(t->data);
		Pre(t->lchild, v);
		Pre(t->rchild, v);
	}
}

void Ino(BitTree t, vector<int> &v){
	if(t == NULL) return;
	else {
		Ino(t->lchild, v);
		v.push_back(t->data);
		Ino(t->rchild, v);
	}
}

int main(){
	int n;
	while(cin >> n){
		if(n == 0) break;
		
		// 建立二叉搜索树
		BitTree T = NULL;
		string s; cin >> s;
		for(int i = 0; i < s.size(); i ++) Insert(T, s[i]-'0');
		
		// 生成对应的前序,中序遍历序列
		vector<int> s_pre;
		vector<...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发