diff options
| author | Andrew <saintruler@gmail.com> | 2021-03-18 18:36:33 +0400 |
|---|---|---|
| committer | Andrew <saintruler@gmail.com> | 2021-03-18 18:36:33 +0400 |
| commit | 14447430a5fae172e0eeff0fd0a484a0fd65e193 (patch) | |
| tree | 4e55fa3d538b549c1d8c50f9fcf0b055a5602f48 | |
| parent | d1b2a66f3246ff6b2d1f22a7b3757345ef429346 (diff) | |
Добавил 17 задачу из деревьев
| -rw-r--r-- | bin-trees/task17.cpp | 53 | ||||
| -rw-r--r-- | bin-trees/tree.h | 19 |
2 files changed, 72 insertions, 0 deletions
diff --git a/bin-trees/task17.cpp b/bin-trees/task17.cpp new file mode 100644 index 0000000..cc917a9 --- /dev/null +++ b/bin-trees/task17.cpp @@ -0,0 +1,53 @@ +#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) +{ + if (n > 0) + { + int x; + cin >> x; + t = node(x); + + // Дерево строится справа налево, чтобы ситуация из задания могла + // случиться (иначе, ответ всегда будет 0) + int nr = n / 2; + int nl = n - nr - 1; + + create(t->right, nr); + create(t->left, nl); + } +} + +int main() +{ + int n; + cin >> n; + tree *t = new tree; + create(t, n); + + // Ответ всегда 0 или 1 + int x = solve(t); + cout << x << endl; + return 0; +} diff --git a/bin-trees/tree.h b/bin-trees/tree.h new file mode 100644 index 0000000..0fa61b1 --- /dev/null +++ b/bin-trees/tree.h @@ -0,0 +1,19 @@ +#pragma once + +struct tree +{ + int inf; + tree *left; + tree *right; + tree *parent; +}; + +tree *node(int x) +{ + tree *n = new tree; + n->inf = x; + n->parent = NULL; + n->right = NULL; + n->left = NULL; + return n; +} |