8,532 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

3 Answers

Best answer
41 votes
41 votes
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
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
1 votes
1 votes

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.

Answer:

Related questions

27 votes
27 votes
3 answers
1
Kathleen asked Sep 18, 2014
8,743 views
Consider the label sequences obtained by the following pairs of traversals on a labeled binary tree. Which of these pairs identify a tree uniquely?preorder and postorderi...
103 votes
103 votes
11 answers
2
62 votes
62 votes
4 answers
3
55 votes
55 votes
8 answers
4