edited by
500 views
0 votes
0 votes
int func(Node  root) {
  int x = 0;
  int y = 0;
  queue Q; 
  Q.push(root);
  while(!Q.empty()) {
    Node u = Q.top(); 
    Q.pop();
    if(u->left == NULL && u->right == NULL) x++;
    if(u->left != NULL) {
      y++;
      Q.push(u->left);
    }
    if(u->right != NULL) {
      y++;
      Q.push(u->right);
    }
  }
  return x+y;
}

Above code segment is executed on the following rooted tree:

What will be the output?

edited by

1 Answer

Best answer
3 votes
3 votes
For each edge we increase y by 1. And for each leaf we increase x by 1.

There are 9 edges & 4 leaves therefore $x+y = 13$.
selected by

Related questions

0 votes
0 votes
1 answer
2
0 votes
0 votes
0 answers
3
eyeamgj asked Oct 29, 2018
253 views
WHAT ARE THE VARIOUS EDGES POSSIBLE DURING BFS ?