题解 | #二叉排序树#
二叉排序树
https://www.nowcoder.com/practice/30a0153649304645935c949df7599602
#include <iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node(int a) : data(a), left(NULL), right(NULL){}
};
// root->a
// a
Node* insert(Node* root, int x, int father){
if(root == NULL){
root = new Node(x);
cout << father << endl;
}
else if (x < root->data) {
root->left = insert(root->left, x, root->data);
}
else {
root->right = insert(root->right, x, root->data);
}
return root;
}
int main() {
int n, x;
while (cin >> n) {
Node* root = NULL;
while (n --) {
cin >> x;
root = insert(root, x, -1);
}
}
return 0;
}
