题解 | #二叉排序树#
二叉排序树
https://www.nowcoder.com/practice/30a0153649304645935c949df7599602
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef struct node {
int num;
struct node* left;
struct node* right;
} node, *tree;
void insert(tree& t, int x, int father) {
if (t == NULL) {
t = (node*)malloc(sizeof(node));
t->num = x;
t->left = NULL;
t->right = NULL;
printf("%d\n", father);
return;
} else {
if (t->num >= x) {
insert(t->left, x, t->num);
} else {
insert(t->right, x, t->num);
}
}
}
void inorder(tree t) {
if (t == NULL) {
return;
}
inorder(t->left);
printf("%d", t->num);
inorder(t->right);
}
int main() {
int n;
while (scanf("%d", &n) != EOF) {
tree root = NULL;
while (n--) {
int x;
scanf("%d", &x);
insert(root, x, -1);
}
}
return 0;
}
查看30道真题和解析

美的集团公司福利 724人发布