blob: d2cf414ad5807537a19c6f95512dd8eefbc99082 (
plain)
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
|
#include <iostream>
#include <string>
#include "tree.h"
using namespace std;
int solve(tree *&t)
{
int count = 0;
if (t->left == NULL && t->right != NULL)
count++;
if (t->left != NULL)
count += solve(t->left);
if (t->right != NULL)
count += solve(t->right);
return count;
}
void create(tree *&t, int n, tree *parent)
{
if (n > 0)
{
int x;
cin >> x;
t = node(x);
t->parent = parent;
// Дерево строится справа налево, чтобы ситуация из задания могла
// случиться хотя бы один раз (иначе, при создании дерева с помощью
// этого алгоритма ответ всегда будет 0)
int nr = n / 2;
int nl = n - nr - 1;
create(t->right, nr, t);
create(t->left, nl, t);
}
}
int main()
{
int n;
cout << "Введите количество узлов: ";
cin >> n;
tree *t = new tree;
cout << "Введите содержимое узлов:" << endl;
create(t, n, NULL);
print(t, log(n) / log(2));
int x = solve(t);
cout << "Количество узлов, имеющих только правого потомка: " << x << endl;
return 0;
}
|