• edited by
189 views
7 7 votes

Which of the following functions correctly returns the total number of nodes in a binary tree rooted at $\texttt{t}$?

  1. int tree_size(TreeNode *t) {
        if (t == NULL)
            return 0;
        else
            return 1 + tree_size(t->left) + tree_size(t->right);
    }
  2. int tree_size(TreeNode *t, int count) {
        if (t == NULL)
            return count;
        else
            return tree_size(t->left, count) + tree_size(t->right, count);
    }
  3. int tree_size(TreeNode *t, int count) {
        if (t == NULL)
            return count;
        else
            return tree_size(t->left, count + 1) + tree_size(t->right, count + 1);
    }
    
  4. int tree_size(TreeNode *t, int count) {
        if (t == NULL)
            return count;
        else
            return tree_size(t->left, count) + tree_size(t->right, count + 1);
    }

1 Answer

0 0 votes

For an empty tree, the number of nodes is $0$.

For a non-empty tree :

$\text{total nodes = 1 + nodes in left subtree + nodes in right subtree}$

Only option A follows this logic correctly :

  • Base Case
    $\texttt{if t == NULL return 0;}$

    If the pointer reaches a dead end (an empty subtree), it contributes $0$ to the total node count.

  • Recursive Step
    $\texttt{return 1 + tree_size(t->left) + tree_size(t->right);}$ 
    If the node exists, it counts itself as $1$, and then recursively adds the total number of nodes in its entire left subtree and its entire right subtree. 

 

B, C, and D attempt to use an accumulator variable $(\texttt{count})$, but they all implement the logic incorrectly : 

  • B : Never actually increments $\texttt{count}$ or adds $1$ for the current node.
    It just passes the same $\texttt{count}$ value down and adds the results, which will yield an incorrect sum.

  • C : Increments $\texttt{count}$ for both the left and right recursive calls and then adds them together.
    If you pass a tree with exactly $1$ node $($and an initial count of $0)$, it will $\texttt{return (0 + 1) + (0 + 1) = 2}$, which is wrong.

  • D : Asymmetrically increments the count (only adding to the right subtree), which makes no logical sense for calculating the total size of a tree.

Answer:
Position:
Show:

Related questions

6 6 votes
1 1 answer
224
224 views
GO Classes asked Jul 13
224 views
A binary tree has:Left subtree containing $1000$ nodes Right subtree containing $100$ nodesHow many nodes are processed before the root in preorder, inorder, and postorde...
6 6 votes
1 1 answer
183
183 views
GO Classes asked Jul 13
183 views
Consider the following binary tree:Which option correctly gives the preorder, postorder, inorder, and level-order traversals?Preorder $:\texttt{9 15 23 12 8 6 2 7 10 5 35...
5 5 votes
2 2 answers
226
226 views
GO Classes asked Jul 13
226 views
A binary tree has the following traversals:Preorder traversal $: \text{A M P K L D H T}$ Inorder traversal $:\text{P M L K A H T D}$Which of the following is the postorde...
6 6 votes
1 1 answer
161
161 views
GO Classes asked Jul 13
161 views
Consider the following binary search tree:If we traverse the tree in postorder and print only the key values that are greater than $12$ and less than $20$, what will be t...