• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

Top MCQs on Tree Traversal with Interview Question and Answers | DSA Quiz

Question 1

Following function is supposed to calculate the maximum depth or height of a Binary tree -- the number of nodes along the longest path from the root node down to the farthest leaf node. C
int maxDepth(struct node* node)
{
   if (node==NULL)
       return 0;
   else
   {
       /* compute the depth of each subtree */
       int lDepth = maxDepth(node->left);
       int rDepth = maxDepth(node->right);
 
       /* use the larger one */
       if (lDepth > rDepth)
           return X;
       else return Y;
   }
}
What should be the values of X and Y so that the function works correctly?
  • X = lDepth, Y = rDepth
  • X = lDepth + 1, Y = rDepth + 1
  • X = lDepth - 1, Y = rDepth -1
  • None of the above

Question 2

What is common in three different types of traversals (Inorder, Preorder and Postorder)?
  • Root is visited before right subtree
  • Left subtree is always visited before right subtree
  • Root is visited after left subtree
  • All of the above
  • None of the above

Question 3

The inorder and preorder traversal of a binary tree are d b e a f c g and a b d e c f g, respectively. The postorder traversal of the binary tree is:
  • d e b f g c a
  • e d b g f c a
  • e d b f g c a
  • d e f g b c a

Question 4

What does the following function do for a given binary tree? C
int fun(struct node *root)
{
   if (root == NULL)
      return 0;
   if (root->left == NULL && root->right == NULL)
      return 0;
   return 1 + fun(root->left) + fun(root->right);
}
  • Counts leaf nodes
  • Counts internal nodes
  • Returns height where height is defined as number of edges on the path from root to deepest node
  • Return diameter where diameter is number of edges on the longest path between any two nodes.

Question 5

Which of the following pairs of traversals is not sufficient to build a binary tree from the given traversals?
  • Preorder and Inorder
  • Preorder and Postorder
  • Inorder and Postorder
  • None of the Above

Question 6

Consider two binary operators \'[Tex]\\uparrow[/Tex] \' and \'[Tex]\\downarrow[/Tex]\' with the precedence of operator [Tex]\\downarrow[/Tex] being lower than that of the [Tex]\\uparrow[/Tex] operator. Operator [Tex]\\uparrow[/Tex] is right associative while operator [Tex]\\downarrow[/Tex] is left associative. Which one of the following represents the parse tree for expression (7 [Tex]\\downarrow[/Tex] 3 ­[Tex]\\uparrow[/Tex] 4 ­[Tex]\\uparrow[/Tex] 3 [Tex]\\downarrow[/Tex] 2)? (GATE CS 2011) gate_2011_5
  • A
  • B
  • C
  • D

Question 7

Which traversal of tree resembles the breadth first search of the graph?
  • Preorder
  • Inorder
  • Postorder
  • Level order

Question 8

Which of the following tree traversal uses a queue data structure?
  • Preorder
  • Inorder
  • Postorder
  • Level order

Question 9

Which of the following cannot generate the full binary tree?
  • Inorder and Preorder
  • Inorder and Postorder
  • Preorder and Postorder
  • None of the above

Question 10

Consider the following C program segment c
struct CellNode
{
  struct CelINode *leftchild;
  int element;
  struct CelINode *rightChild;
}

int Dosomething(struct CelINode *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 DoSomething when a pointer to the root of a non-empty tree is passed as argument is (GATE CS 2004)
  • The number of leaf nodes in the tree
  • The number of nodes in the tree
  • The number of internal nodes in the tree
  • The height of the tree

There are 42 questions to complete.

Last Updated :
Take a part in the ongoing discussion