-
Notifications
You must be signed in to change notification settings - Fork 0
/
1115.cpp
62 lines (52 loc) · 1.32 KB
/
1115.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <cstdio>
#include <climits>
#include <algorithm>
int maxDepth = 0;
struct BST {
int data;
BST *lc, *rc;
int depth;
BST(int data) : data(data) {
depth = 0;
lc = rc = nullptr;
}
void insert(BST *newNode) {
if (newNode->data <= this->data) {
if (!lc) {
lc = newNode;
lc->depth = this->depth + 1;
maxDepth = std::max(maxDepth, lc->depth);
}
else lc->insert(newNode);
} else {
if (!rc) {
rc = newNode;
rc->depth = this->depth + 1;
maxDepth = std::max(maxDepth, rc->depth);
}
else rc->insert(newNode);
}
}
};
int a, b;
void dfs(BST *root) {
if (root->depth == maxDepth) a++;
else if (root->depth == maxDepth - 1 && root->depth != 0) b++;
if (root->lc) dfs(root->lc);
if (root->rc) dfs(root->rc);
}
int main() {
int n;
scanf("%d", &n);
BST *root = new BST(INT_MIN);
root->depth = 0;
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
root->insert(new BST(x));
}
// printf("%d\n", maxDepth);
dfs(root);
printf("%d + %d = %d\n", a, b, a + b);
return 0;
}