int isSumTree(struct node* node) { int ls, rs; if(node == NULL||(node->left == NULL && node->right == NULL)) return 1; ls = sumnode(node->left); rs = sumnode(node->right); if((node->data== (ls+rs))&& isSumTree(node->left) && isSumTree(node->right)) return 1; return 0; }
local void gravsum(bodyptr p0, cellptr cptr, cellptr bptr) { vector pos0, acc0; real phi0; SETV(pos0, Pos(p0)); // copy position of body phi0 = 0.0; // init total potential CLRV(acc0); // and total acceleration if (usequad) // if using quad moments sumcell(interact, cptr, pos0, &phi0, acc0); // sum cell forces w quads else // not using quad moments sumnode(interact, cptr, pos0, &phi0, acc0); // sum cell forces wo quads sumnode(bptr, interact + actmax, pos0, &phi0, acc0); // sum forces from bodies Phi(p0) = phi0; // store total potential SETV(Acc(p0), acc0); // and total acceleration nfcalc++; // update counters nbbcalc += interact + actmax - bptr; nbccalc += cptr - interact; }
local void gravsum(bodyptr p0, cellptr cptr, cellptr bptr) { vector pos0, acc0; real phi0; SETV(pos0, Pos(p0)); /* copy position of body */ phi0 = 0.0; /* init total potential */ CLRV(acc0); /* and total acceleration */ if (usequad) /* if using quad moments */ sumcell(interact, cptr, pos0, &phi0, acc0); /* sum cell forces w quads */ else /* not using quad moments */ sumnode(interact, cptr, pos0, &phi0, acc0); /* sum cell forces wo quads */ sumnode(bptr, interact + actlen, pos0, &phi0, acc0); /* sum forces from bodies */ Phi(p0) = phi0; /* store total potential */ SETV(Acc(p0), acc0); /* and total acceleration */ nfcalc++; /* update counters */ nbbcalc += interact + actlen - bptr; nbccalc += cptr - interact; }
int main() { struct node *root = newNode(5); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(1); root->left->right = newNode(4); root->right->left = newNode(6); root->right->right = newNode(7); printf(" total sum of node is %d ", sumnode(root)); return 0; }
int sumnode(struct node *root) { if(root==NULL) return; return (root->data + sumnode(root->left) + sumnode(root->right)); }