Inorder Traversal | Data Structure MCQs

1.

Select the code snippet which performs level-order traversal.

   A.)
public static void levelOrder(Tree root) 
{  
    Queue<Node> queue=new LinkedList<Node>();  
    queue.add(root);  
    while(!queue.isEmpty())  
    {  
        Node tempNode=queue.poll();  
        System.out.println("%d ",tempNode.data);  
        if(tempNode.left!=null)  
        queue.add(tempNode.left);  
        if(tempNode.right!=null)  
        queue.add(tempNode.right);  
    }  
}
   B.)
public static void levelOrder(Tree root) 
{  
    Queue<Node> queue=new LinkedList<Node>();  
    queue.add(root);  
    while(!queue.isEmpty())  
    {  
        Node tempNode=queue.poll();  
        System.out.println("%d ",tempNode.data);  
        if(tempNode.left!=null)  
        queue.add(tempNode.right);  
        if(tempNode.right!=null)  
        queue.add(tempNode.left);  
    }  
}
   C.)
public static void levelOrder(Tree root) 
{  
    Queue<Node> queue=new LinkedList<Node>();  
    queue.add(root);  
    while(!queue.isEmpty())  
    {  
        Node tempNode=queue.poll();  
        System.out.println("%d ",tempNode.data);  
        if(tempNode.right!=null)  
        queue.add(tempNode.left);  
        if(tempNode.left!=null)  
        queue.add(tempNode.right);  
    }  
}
   D.) None of these

Answer: Option 'A'

Firstly add the root node to the queue. Then for all the remaining nodes, pop the front end of the queue and print it, add the left and right children of the popped node to the queue.

2.

Select the code snippet which performs in-order traversal.

   A.)
public void inorder(Tree root)
{
	System.out.println(root.data);
	inorder(root.left);
	inorder(root.right);
}
   B.)
public void inorder(Tree root)
{
	inorder(root.left);
	System.out.println(root.data);
	inorder(root.right);
}
   C.)
public void inorder(Tree root)
{
	System.out.println(root.data);
	inorder(root.right);
	inorder(root.left);
}
   D.) None of these

Answer: Option 'B'

In-order traversal follows LNR(Left-Node-Right).

3.

What is the time complexity of level order traversal?

   A.) O(1)
   B.) O(n)
   C.) O(logn)
   D.) O(nlogn)

Answer: Option 'B'

Since you have to go through all the nodes, the complexity becomes O(n).

4.

Which of the following graph traversals closely imitates level order traversal of a binary tree?

   A.) Depth First Search
   B.) Breadth First Search
   C.) Both of the mentioned
   D.) None of these

Answer: Option 'B'

Both level order tree traversal and breadth first graph traversal follow the principle that visit your neighbors first and then move on to further nodes.

5.

What is the space complexity of the in-order traversal in the recursive fashion? (d is the tree depth and n is the number of nodes)

   A.) O(1)
   B.) O(nlogd)
   C.) O(logd)
   D.) O(d)

Answer: Option 'D'

In the worst case we have d stack frames in the recursive call, hence the complexity is O(d).

6.

For the tree below, write the in-order traversal.

   A.) 2, 7, 2, 6, 5, 11, 5, 9, 4
   B.) 2, 5, 11, 6, 7, 4, 9, 5, 2
   C.) 2, 7, 5, 2, 6, 9, 5, 11, 4
   D.) 2, 7, 5, 6, 11, 2, 5, 4, 9

Answer: Option 'D'

In-order traversal follows LNR(Left-Node-Right).

7.

In a binary search tree, which of the following traversals would print the numbers in the ascending order?

   A.) Level-order traversal
   B.) Pre-order traversal
   C.) Post-order traversal
   D.) In-order traversal

Answer: Option 'D'

In a binary search tree, a node’s left child is always lesser than the node and right child is greater than the node, hence an in-order traversal would print them in a non decreasing fashion.

8.

For the tree below, write the level-order traversal.

   A.) 2, 7, 2, 6, 5, 11, 5, 9, 4
   B.) 2, 7, 5, 2, 6, 9, 5, 11, 4
   C.) 2, 5, 11, 6, 7, 4, 9, 5, 2
   D.) 2, 7, 5, 6, 11, 2, 5, 4, 9

Answer: Option 'B'

Level order traversal follows a breadth first search approach.

Inorder Traversal | Data Structure MCQs Download Pdf