14,104 views
56 56 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
59 59 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
7 7 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 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.

Answer:
Position:
Show:

Related questions

42 42 votes
3 answers 3 answers
14.0k
14.0k views
Kathleen asked Sep 18, 2014
13,990 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...
159 159 votes
15 answers 15 answers
60.2k
60.2k views
Kathleen asked Sep 18, 2014
60,216 views
A program takes as input a balanced binary search tree with $n$ leaf nodes and computes the value of a function $g(x)$ for each node $x$. If the cost of computing $g(x)$ ...
96 96 votes
6 answers 6 answers
29.9k
29.9k views
Kathleen asked Sep 18, 2014
29,854 views
Suppose each set is represented as a linked list with elements in arbitrary order. Which of the operations among $\text{union, intersection, membership, cardinality}$ wil...
82 82 votes
13 answers 13 answers
48.2k
48.2k views
Kathleen asked Sep 18, 2014
48,176 views
A circularly linked list is used to represent a Queue. A single variable $p$ is used to access the Queue. To which node should $p$ point such that both the operations $\t...