in DS
8,571 views
40 votes
40 votes

Consider the following C program segment

struct CellNode{
    struct CellNode *leftChild
    int element;
    struct CellNode *rightChild;
    };

int Dosomething (struct CellNode *ptr)
{
    int value = 0;
    if(ptr != NULL)
    { 
        if (ptr -> leftChild != NULL)
            value = 1 + DoSomething (ptr -> leftChild);
        if (ptr -> rightChild != NULL)
            value = max(value, 1 + Dosomething (ptr -> rightChild));
    }
    return(value);
}

The value returned by the function $\text{DoSomething}$ when a pointer to the root of a non-empty tree is passed as argument is

  1. The number of leaf nodes in the tree
  2. The number of nodes in the tree
  3. The number of internal nodes in the tree
  4. The height of the tree
in DS
8.6k views

3 Comments

1st if calculates the height of the left subtree and 2nd if computes the height of the right subtree and takes the max of (height of left subtree, height of right subtree).

18
18
as program is comparing values from left and right sub tree , hence result can not be count of leaf , internal nodes , total nodes hence option a,b,c can be  eliminated
0
0

Take this tree .

Result for all options will be as follows:

A- 4

B-7

C-3

D-2

Running given code we get 2 as answer . So option D is right

Check for Symmetric Binary Tree (Iterative Approach) in C++

0
0

3 Answers

41 votes
41 votes
Best answer
Correct Option: D

It calculates Height of tree.

Easy way to get this answer .

Draw a tree where all $4$ parameters are different.

Get a Tree for which Height, No of Internal Nodes & No of Leafs are different & Trace out this algorithm.
edited by

1 comment

edited by

The below diagram may make the code easier to understand.

3
3
6 votes
6 votes
It actually calculates the height of the tree

How I did that: I drew a tree then just tried the algo on this tree and then I modified the tree wisely,  then I tried the algo one more time

4 Comments

Wow ... Thug life (y) ...
2
2

 

How I did that: I drew a tree then just tried the algo on this tree and then I modified the tree wisely,  then I tried the algo one more time

what kind of explanation is this? :D :D

12
12
LOL
0
0
there should be a section called “legend answer” for answers like these :P
2
2
1 vote
1 vote

Try to run code on this type of tree:

1.here node at the same level are sibling(are right subtree).

  1. The node below are child(left subtree).

It is kind of representaion of n ary tree.

1 comment

Your figure can be represented as 

You will get answer as D

0
0
Answer:

Related questions