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 child
  • leftchild(): returns node of left child
  • isempty(): 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

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -