#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 1010;
int a[N];
int n;
int cnt;
struct Node{
Node *l;
Node *r;
int val;
};
Node *root = NULL;
Node *build(Node *root, int val){
if (root == NULL){
Node *p = new Node;
p -> val = val;
p -> l = NULL;
p -> r = NULL;
return p;
}
if (root -> val > val){
root -> l = build(root -> l, val);
}
else{
root -> r = build(root -> r, val);
}
return root;
}
void print(Node *t){
if (t == NULL)
return;
print(t -> l);
print(t -> r);
if (t != root){
cout << t -> val << " ";
}
}
int main(){
cin >> n;
for (int i = 1; i <= n; i ++) cin >> a[i];
for (int i = 1; i <= n; i ++){
root = build(root, a[i]);
}
print(root);
cout << root -> val << endl;
return 0;
}
二叉搜索树的建图方式
于 2021-04-20 14:06:47 首次发布