RESULT tree_sum(int sum) { int ch, n; RESULT left, right; /* discard open-parenthesis */ discard_space(), getchar(); discard_space(); ch = getchar(); if(isdigit(ch) || ch == '-' || ch == '+') { ungetc(ch, stdin); scanf("%d", &n); left = tree_sum(sum - n); right = tree_sum(sum - n); /* discard closed-parenthesis */ discard_space(), getchar(); if(left == EMPTY && right == EMPTY && sum == n) return YES_PATH; else if(left == YES_PATH || right == YES_PATH) return YES_PATH; return NO_PATH; } return EMPTY; }
int main() { int s; while(scanf("%d", &s) > 0) { if(tree_sum(s) == YES_PATH) printf("yes\n"); else printf("no\n"); } return 0; }
int main(int argc, const char *argv[]) { tree l = new_tree(10, new_tree(8, new_tree(6, new_tree(-4, NULL, NULL), new_tree(-2, NULL, NULL)), NULL), new_tree(8, new_tree(6, NULL, NULL), NULL)); tree_print(l); printf("\n\n"); tree_iterative_traverse(print_int, l); tree_clear_zero_sum(l); tree_print(l); printf("\n\n"); printf("%i\n", tree_sum(l->left->left)); return 0; }
int tree_sum(tree_t tree) { if (tree_isEmpty(tree)) return 0; return tree_elt(tree) + tree_sum(tree_left(tree)) + tree_sum(tree_right(tree)); }
bool tree_zero_sum(tree xs) { // {{{ return tree_sum(xs) == 0; } // }}}
int tree_sum(tree xs) { // {{{ if (tree_empty(xs)) return 0; return xs->value + tree_sum(xs->left) + tree_sum(xs->right); } // }}}