-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST (2).cpp
More file actions
49 lines (49 loc) · 1.22 KB
/
BST (2).cpp
File metadata and controls
49 lines (49 loc) · 1.22 KB
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
45
46
47
48
49
//BST
struct node
{
int val, subsize;
node *left, *right;
node(int v) {val = v, subsize = 1, left = right = NULL;}
};
class BST
{
node *head;
void insert(int val, node *now)
{
++now->subsize;
if (val < now->val)
{
if (now->left == NULL)
{
now->left = new node(val);
return;
}
else insert(val, now->left);
}
else
{
if (now->right == NULL)
{
now->right = new node(val);
return;
}
else insert(val, now->right);
}
}
long long calc(node *now)
{
if (now == NULL) return 1;
long long left, right, ret;
left = calc(now->left);
right = calc(now->right);
ret = (left * right) % MOD;
if (now->left != NULL && now->right != NULL) ret = ret * rec(now->left->subsize, now->right->subsize);
if (now->left != NULL) delete now->left;
if (now->right != NULL) delete now->right;
return ret % MOD;
}
public:
BST(int n) { head = new node(n); }
void insert(int n) { insert(n,head); }
long long calc() { return calc(head); }
};