c++ - Count binary tree left and right children separately -
i have code counts number of right children of binary tree.
int count(arbin<t> a){ if(a.isempty()) return 0; int num_l=0, num_r=0; if(!a.leftchild.isempty()) num_l = count(a.leftchild()); if(!a.rightchild.isempty()) num_r = count(a.rightchild())+1; return num_l+num_r;}
how can modify can number of right children , number of left children separately?
arbin
class allow me operations (and can not modify it):
rightchild()
: returns node of right childleftchild()
: returns node of left childisempty()
: returns whether node empty or not
if can modify count()
following:
void count(arbin<t> a, int& l, int& r){ if(a.isempty()) return ; if(!a.leftchild.isempty()) count(a.leftchild(), l+1, r); if(!a.rightchild.isempty()) count(a.rightchild(), l, r+1); }
now call count
following
int l = 0, r = 0; count(tree, l, r); // here you've left child count in `l` , right count in `r`
Comments
Post a Comment