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

Last Updated : 27 Sep, 2023

A Tree Data Structure can be traversed in following ways:

  • Inorder Traversal
  • Preorder Traversal
  • Postorder Traversal

More on Tree traversal

Tree Traversal

Tree Traversal

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.
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?
Cross
X = lDepth, Y = rDepth
Tick
X = lDepth + 1, Y = rDepth + 1
Cross
X = lDepth - 1, Y = rDepth -1
Cross
None of the above


Question 1-Explanation: 
If a tree is not empty, height of tree is MAX(Height of Left Subtree, Height of Right Subtree) + 1 See program to Find the Maximum Depth or Height of a Tree for more details.
Question 2
What is common in three different types of traversals (Inorder, Preorder and Postorder)?
Cross
Root is visited before right subtree
Tick
Left subtree is always visited before right subtree
Cross
Root is visited after left subtree
Cross
All of the above
Cross
None of the above


Question 2-Explanation: 
The order of inorder traversal is LEFT ROOT RIGHT The order of preorder traversal is ROOT LEFT RIGHT The order of postorder traversal is LEFT RIGHT ROOT In all three traversals, LEFT is traversed before RIGHT
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:
Tick
d e b f g c a
Cross
e d b g f c a
Cross
e d b f g c a
Cross
d e f g b c a


Question 3-Explanation: 
Below is the given tree.
                              a
                           /    \\
                        /          \\
                      b             c
                   /   \\          /   \\
                 /       \\      /       \\
               d         e    f          g
Question 4
What does the following function do for a given binary tree?
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);
}
Cross
Counts leaf nodes
Tick
Counts internal nodes
Cross
Returns height where height is defined as number of edges on the path from root to deepest node
Cross
Return diameter where diameter is number of edges on the longest path between any two nodes.


Question 4-Explanation: 
The function counts internal nodes. 1) If root is NULL or a leaf node, it returns 0. 2) Otherwise returns, 1 plus count of internal nodes in left subtree, plus count of internal nodes in right subtree. See the following complete program.
Question 5
Which of the following pairs of traversals is not sufficient to build a binary tree from the given traversals?
Cross
Preorder and Inorder
Tick
Preorder and Postorder
Cross
Inorder and Postorder
Cross
None of the Above


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


Question 6-Explanation: 
Let us consider the given expression (7 \\downarrow 3 \\uparrow 4 \\uparrow 3 \\downarrow 2). Since the precedence of \\uparrow is higher, the sub-expression (3 \\uparrow 4 \\uparrow 3) will be evaluated first. In this sub-expression, 4 \\uparrow 3 would be evaluated first because \\uparrow is right to left associative. So the expression is evaluated as ((7 \\downarrow (3 \\uparrow (4 \\uparrow 3))) \\downarrow 2). Also, note that among the two \\downarrow operators, first one is evaluated before the second one because the associativity of \\downarrow is left to right.
Question 7
Which traversal of tree resembles the breadth first search of the graph?
Cross
Preorder
Cross
Inorder
Cross
Postorder
Tick
Level order


Question 7-Explanation: 
Breadth first search visits all the neighbors first and then deepens into each neighbor one by one. The level order traversal of the tree also visits nodes on the current level and then goes to the next level.
Question 8
Which of the following tree traversal uses a queue data structure?
Cross
Preorder
Cross
Inorder
Cross
Postorder
Tick
Level order


Question 8-Explanation: 
Level order traversal uses a queue data structure to visit the nodes level by level.
Question 9
Which of the following cannot generate the full binary tree?
Cross
Inorder and Preorder
Cross
Inorder and Postorder
Cross
Preorder and Postorder
Tick
None of the above


Question 9-Explanation: 
To generate a binary tree, two traversals are necessary and one of them must be inorder. But, a full binary tree can be generated from preorder and postorder traversals. Read the algorithm here. Read Can tree be constructed from given traversals?
Question 10
Consider the following C program segment
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)
Cross
The number of leaf nodes in the tree
Cross
The number of nodes in the tree
Cross
The number of internal nodes in the tree
Tick
The height of the tree


Question 10-Explanation: 
Explanation: DoSomething() returns max(height of left child + 1, height of right child + 1). So given that pointer to root of tree is passed to DoSomething(), it will return height of the tree. Note that this implementation follows the convention where height of a single node is 0.
There are 42 questions to complete.


Share your thoughts in the comments

Similar Reads