-
Notifications
You must be signed in to change notification settings - Fork 0
/
1115.cpp
74 lines (66 loc) · 1.07 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
63
64
65
66
67
68
69
70
71
72
73
74
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
vector<int> v;
struct node{
int data;
node* lc;
node* rc;
int layer;
};
node* newNode(int x){
node* root = new node;
root->data = x;
root->lc = root->rc = NULL;
return root;
}
void insert(node* &root, int x){
if(root == NULL){
root = newNode(x);
return;
}
if(x <= root->data){
insert(root->lc, x);
}
else{
insert(root->rc, x);
}
}
void bfs(node* root){
queue<node*> q;
root->layer = 0;
q.push(root);
while(!q.empty()){
node* top = q.front();
q.pop();
if(v.size() <= top->layer){
v.push_back(1);
}
else{
v[top->layer]++;
}
if(top->lc != NULL){
top->lc->layer = top->layer + 1;
q.push(top->lc);
}
if(top->rc != NULL){
top->rc->layer = top->layer + 1;
q.push(top->rc);
}
}
}
int main(){
int N;
scanf("%d", &N);
node* root = NULL;
for(int i = 0; i < N; i++){
int temp;
scanf("%d", &temp);
insert(root, temp);
}
bfs(root);
if(v.size())
printf("%d + %d = %d\n", v[v.size() - 1], v[v.size() - 2], v[v.size() - 1] + v[v.size() - 2]);
return 0;
}