50106. Construct a Binary Search Tree

難度:2.5/5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "construct.h"

// typedef struct Node{
// char name[16];
// int height;
// int weight;
// struct Node *left, *right;
// } Node;

Node* insert(Node* root, char* name, int h, int w){
if(root == NULL){
Node* now = (Node*) malloc(sizeof(Node));
strcpy(now->name, name);
now->height = h;
now->weight = w;
now->left = NULL;
now->right = NULL;
return now;
}
else{
#ifdef HEIGHT
if(h < root->height) root->left = insert(root->left, name, h, w);
else root->right = insert(root->right, name, h, w);
#endif
#ifdef WEIGHT
if(w < root->weight) root->left = insert(root->left, name, h, w);
else root->right = insert(root->right, name, h, w);
#endif
return root;
}
}

Node *ConstructBSTree(int N, char name[][16], int height[], int weight[]){
Node* root = NULL;
for(int i = 0; i < N; i++){
root = insert(root, name[i], height[i], weight[i]);
}
return root;
}


50106. Construct a Binary Search Tree
https://aaronlin1229.github.io/judgegirl_50106/
Author
Akizumi
Posted on
July 17, 2023
Licensed under