Determine the Internal Path Length of a Tree (C++) -
well i'm down last function of program , i'm done. i've hit stump can't seem fix on own.
int tree::internalpathlength(node * r, int value) { if(r->left == null && r->right == null) { return 0; } return value + internalpathlength(r->left, value+1) + internalpathlength(r->right, value+1); }
i feel i'm close solution, know i'm missing something. think it's if statement , i've tried different combinations end getting crashed program or 0 answer.
any suggestions or appreciated ! thanks!
may works:
int tree::internalpathlength(node * r, int value) { if(r->left == null && r->right == null) { return 0; } return value + ( r->left? internalpathlength(r->left, value+1):0 ) + ( r->right? internalpathlength(r->right, value+1):0 ); }
or add null check node
int tree::internalpathlength(node * r, int value) { if (r == null ) return 0; if(r->left == null && r->right == null) { return value +1; } return value + internalpathlength(r->left, value+1) + internalpathlength(r->right, value+1); }
Comments
Post a Comment