Untitled
unknown
plain_text
4 years ago
1.1 kB
3
Indexable
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> typedef struct node_ { struct node_* l, * r; int val; } node; node* build() { node* x = (node*)malloc(sizeof(node)); char c = getc(stdin); x->l = 0, x->r = 0; if (isdigit(c)) { ungetc(c, stdin); x->l = (node*)malloc(sizeof(node)); x->l->r = 0; x->l->l = 0; scanf("%d", &x->l->val); } else x->l = build(); while ((c = getc(stdin)) == ' '); if (c == EOF || c == '\n') return x->l; if (isdigit(c)) { ungetc(c, stdin); x->r = (node*)malloc(sizeof(node)); x->r->r = 0; x->r->l = 0; scanf("%d", &x->r->val); } else x->r = build(); getc(stdin); return x; } double solve(node* n) { if (!n) return 0; int cnt = !!(n->l) + !!(n->r); if (!cnt) return n->val; double sum = solve(n->l) + solve(n->r); return sum / cnt; } void freee(node* n) { if (!n) return; freee(n->l); freee(n->r); free(n); } signed main() { node* d = build(); printf("%.2lf", solve(d)); freee(d); }
Editor is loading...