# Inorder Traversal | Data Structure MCQs

1.

Select the code snippet which performs level-order traversal.

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

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

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)

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
C.) Both of the mentioned
D.) None of these

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)

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

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

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