#include #include #include "tree.h" using namespace std; void helper(tree *&t, int *sum, int *cnt) { if (!t) return; if (t->left == NULL && t->right == NULL) { *sum += t->inf; (*cnt)++; } helper(t->left, sum, cnt); helper(t->right, sum, cnt); } float solve(tree *&t) { int sum = 0; int cnt = 0; helper(t, &sum, &cnt); return sum * 1.0 / cnt; } void create(tree *&t, int n, tree *parent) { if (n > 0) { int x; cin >> x; t = node(x); t->parent = parent; int nl = n / 2; int nr = n - nl - 1; create(t->left, nl, t); create(t->right, nr, t); } } int main() { int n; cout << "Введите количество узлов: "; cin >> n; tree *t = new tree; cout << "Введите содержимое узлов:" << endl; create(t, n, NULL); print(t, log(n) / log(2)); float res = solve(t); cout << "Среднее арифметическое листьев равно " << res << endl; return 0; }