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?
A
X = lDepth, Y = rDepth
B
X = lDepth + 1, Y = rDepth + 1
C
X = lDepth - 1, Y = rDepth -1
D
None of the above
Tree Traversals    
Discuss it


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)?
A
Root is visited before right subtree
B
Left subtree is always visited before right subtree
C
Root is visited after left subtree
D
All of the above
E
None of the above
Tree Traversals    
Discuss it


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:
A
d e b f g c a
B
e d b g f c a
C
e d b f g c a
D
d e f g b c a
Tree Traversals    
Discuss it


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);
}
A
Counts leaf nodes
B
Counts internal nodes
C
Returns height where height is defined as number of edges on the path from root to deepest node
D
Return diameter where diameter is number of edges on the longest path between any two nodes.
Tree Traversals    
Discuss it


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. 1
Question 5
Which of the following pairs of traversals is not sufficient to build a binary tree from the given traversals?
A
Preorder and Inorder
B
Preorder and Postorder
C
Inorder and Postorder
D
None of the Above
Tree Traversals    
Discuss it


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
A
A
B
B
C
C
D
D
Tree Traversals    
Discuss it


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


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?
A
Preorder
B
Inorder
C
Postorder
D
Level order
Tree Traversals    
Discuss it


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?
A
Inorder and Preorder
B
Inorder and Postorder
C
Preorder and Postorder
D
None of the above
Tree Traversals    
Discuss it


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)
A
The number of leaf nodes in the tree
B
The number of nodes in the tree
C
The number of internal nodes in the tree
D
The height of the tree
Tree Traversals    
Discuss it


Question 10 Explanation: 
Explanation: DoSomething() returns max(height of left child + 1, height of left 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.
Question 11
Let LASTPOST, LASTIN and LASTPRE denote the last vertex visited in a postorder, inorder and preorder traversal. Respectively, of a complete binary tree. Which of the following is always true? (GATE CS 2000)
A
LASTIN = LASTPOST
B
LASTIN = LASTPRE
C
LASTPRE = LASTPOST
D
None of the above
Tree Traversals    
Discuss it


Question 11 Explanation: 
It is given that the given tree is complete binary tree. For a complete binary tree, the last visited node will always be same for inorder and preorder traversal. None of the above is true even for a complete binary tree. The option (a) is incorrect because the last node visited in Inorder traversal is right child and last node visited in Postorder traversal is root. The option (c) is incorrect because the last node visited in Preorder traversal is right child and last node visited in Postorder traversal is root. For option (b), see the following counter example. Thanks to Hunaif Muhammed for providing the correct explanation.
     1
   /    \
  2      3
 / \    /
4   5  6  

Inorder traversal is 4 2 5 1 6 3
Preorder traversal is 1 2 4 5 3 6 
Question 12
The array representation of a complete binary tree contains the data in sorted order. Which traversal of the tree will produce the data in sorted form?
A
Preorder
B
Inorder
C
Postorder
D
Level order
Tree Traversals    
Discuss it


Question 12 Explanation: 
The level order traversal of a binary tree prints the data in the same order as it is stored in the array representation of a complete binary tree.
Question 13
Consider the following rooted tree with the vertex P labeled as root GATECS2014Q19 The order in which the nodes are visited during in-order traversal is
A
SQPTRWUV
B
SQPTURWV
C
SQPTWUVR
D
SQPTRUWV
Tree Traversals    GATE-CS-2014-(Set-3)    
Discuss it


Question 13 Explanation: 
Algorithm Inorder(tree) - Use of Recursion
Steps:
1. Traverse the left subtree, 
   i.e., call Inorder(left-subtree)
2. Visit the root.
3. Traverse the right subtree, 
   i.e., call Inorder(right-subtree)

Understanding this algorithm requires the basic 
understanding of Recursion

Therefore, We begin in the above tree with root as
the starting point, which is P.

# Step 1( for node P) :
Traverse the left subtree of node or root P.
So we have node Q on left of P.

-> Step 1( for node Q)
Traverse the left subtree of node Q.
So we have node S on left of Q.

* Step 1 (for node S)
Now again traverse the left subtree of node S which is 
NULL here.

* Step 2(for node S)
Visit the node S, i.e print node S as the 1st element of 
inorder traversal.

* Step 3(for node S)
Traverse the right subtree of node S.
Which is NULL here.

Now move up in the tree to Q which is parent
of S.( Recursion, function of Q called for function of S).
Hence we go back to Q.

-> Step 2( for node Q):
Visit the node Q, i.e print node Q as the 2nd
element of inorder traversal.

-> Step 3 (for node Q)
Traverse the right subtree of node Q.
Which is NULL here.

Now move up in the tree to P which is parent
of Q.( Recursion, function of P called for function of Q).
Hence we go back to P.

# Step 2(for node P)
Visit the node P, i.e print node S as the 3rd
element of inorder traversal.

# Step 3 (for node P)
Traverse the right subtree of node P.
Node R is at the right of P.

Till now we have printed SQP as the inorder of the tree. 
Similarly other elements can be obtained by traversing 
the right subtree of P.

The final correct order of Inorder traversal would 
be SQPTRWUV. 
Question 14
Consider the pseudocode given below. The function DoSomething() takes as argument a pointer to the root of an arbitrary tree represented by the leftMostChild-rightSibling representation. Each node of the tree is of type treeNode.
typedef struct treeNode* treeptr;
struct treeNode
{
    treeptr leftMostChild, rightSibling;
};
int DoSomething (treeptr tree)
{
    int value=0;
    if (tree != NULL)
    {
        if (tree->leftMostChild == NULL)
            value = 1;
        else
            value = DoSomething(tree->leftMostChild);
        value = value + DoSomething(tree->rightSibling);
    }
    return(value);
}

When the pointer to the root of a tree is passed as the argument to DoSomething, the value returned by the function corresponds to the
A
number of internal nodes in the tree.
B
height of the tree.
C
number of nodes without a right sibling in the tree.
D
number of leaf nodes in the tree.
Tree Traversals    GATE-CS-2014-(Set-3)    
Discuss it


Question 14 Explanation: 
The function counts leaf nodes for a tree represented using leftMostChild-rightSibling representation. Below is function with comments added to demonstrate how function works. 1
Question 15
Level order traversal of a rooted tree can be done by starting from the root and performing
A
preorder traversal
B
inorder traversal
C
depth first search
D
breadth first search
Tree Traversals    GATE-CS-2004    
Discuss it


Question 15 Explanation: 
See this post for details
Question 16
Consider the label sequences obtained by the following pairs of traversals on a labeled binary tree. Which of these pairs identify a tree uniquely ?
(i)     preorder and postorder
(ii)    inorder and postorder
(iii)   preorder and inorder
(iv)   level order and postorder
A
(i) only
B
(ii), (iii)
C
(iii) only
D
(iv) only
Tree Traversals    GATE-CS-2004    
Discuss it


Question 16 Explanation: 
Here, we consider each and every option to check whether it is true or false. 1) Preorder and postorder anil_ds_35 For the above trees, Preorder is AB Postorder is BA It shows that preorder and postorder can’t identify a tree uniquely. 2) Inorder and postorder define a tree uniquely 3) Preorder and Inorder also define a tree uniquely 4) Levelorder and postorder can’t define a tree uniquely. For the above example, Level order is AB Postorder is BA See http://www.geeksforgeeks.org/if-you-are-given-two-traversal-sequences-can-you-construct-the-binary-tree/ for details This solution is contributed by Anil Saikrishna Devarasetty
Question 17
Let LASTPOST, LASTIN and LASTPRE denote the last vertex visited in a postorder, inorder and preorder traversal, respectively, of a complete binary tree. Which of the following is always true?
A
LASTIN = LASTPOST
B
LASTIN = LASTPRE
C
LASTPRE = LASTPOST
D
None of the above
Tree Traversals    GATE-CS-2000    
Discuss it


Question 17 Explanation: 
Question 18
Which one of the following binary trees has its inorder and preorder traversals as BCAD  and ABCD, respectively?
tree
A
A
B
B
C
C
D
D
Tree Traversals    GATE-IT-2004    
Discuss it


Question 18 Explanation: 
Inorder Traversal: Left -Root -Right PreOrder Traversal: Root-Left-Right
    InOrder  PreOrder

A.   BADC   ABCD

B.   BCAD   ACBD

C.   ACBD   ABCD
D.    BCAD  ABCD Therefore, D is Correct
Question 19
The numbers 1, 2, .... n are inserted in a binary search tree in some order. In the resulting tree, the right subtree of the root contains p nodes. The first number to be inserted in the tree must be
A
p
B
p + 1
C
n - p
D
n - p + 1
Tree Traversals    Gate IT 2005    
Discuss it


Question 19 Explanation: 
Binary Search Tree, is a node-based binary tree data structure which has the following properties:
  • The left subtree of a node contains only nodes with keys less than the node’s key.
  • The right subtree of a node contains only nodes with keys greater than the node’s key.
  • The left and right subtree each must also be a binary search tree. There must be no duplicate nodes.
So let us say n=10, p=4. According to BST property the root must be 10-4=6 (considering all unique elements in BST)
And according to BST insertion, root is the first element to be inserted in a BST.
Therefore, the answer is (n-p).
 
Question 20
A binary search tree contains the numbers 1, 2, 3, 4, 5, 6, 7, 8. When the tree is traversed in pre-order and the values in each node printed out, the sequence of values obtained is 5, 3, 1, 2, 4, 6, 8, 7. If the tree is traversed in post-order, the sequence obtained would be  
A
8, 7, 6, 5, 4, 3, 2, 1
B
1, 2, 3, 4, 8, 7, 6, 5
C
2, 1, 4, 3, 6, 7, 8, 5
D
2, 1, 4, 3, 7, 8, 6, 5
Tree Traversals    Gate IT 2005    
Discuss it


Question 20 Explanation: 
Question 21
If all the edge weights of an undirected graph are positive, then any subset of edges that connects all the vertices and has minimum total weight is a  
A
Hamiltonian cycle
B
grid
C
hypercube
D
tree
Tree Traversals    Graph Theory    GATE IT 2006    
Discuss it


Question 21 Explanation: 
  As here we want subset of edges that connects all the vertices and has minimum total weight i.e. Minimum Spanning Tree Option A - includes cycle, so may or may not connect all edges. Option B - has no relevance to this question. Option C - includes cycle, so may or may not connect all edges. Related: http://www.geeksforgeeks.org/greedy-algorithms-set-2-kruskals-minimum-spanning-tree-mst/ http://www.geeksforgeeks.org/greedy-algorithms-set-5-prims-minimum-spanning-tree-mst-2/ This solution is contributed by Mohit Gupta.  
Question 22
When searching for the key value 60 in a binary search tree, nodes containing the key values 10, 20, 40, 50, 70 80, 90 are traversed, not necessarily in the order given. How many different orders are possible in which these key values can occur on the search path from the root to the node containing the value 60?
A
35
B
64
C
128
D
5040
Binary Search Trees    Tree Traversals    Gate IT 2007    
Discuss it


Question 22 Explanation: 
There are two set of values, smaller than 60 and greater than 60. Smaller values 10, 20, 40 and 50 are visited, means they are visited in order. Similarly, 90, 80 and 70 are visited in order.
= 7!/(4!3!)
= 35
Question 23
The following three are known to be the preorder, inorder and postorder sequences of a binary tree. But it is not known which is which.
MBCAFHPYK
KAMCBYPFH
MABCKYFPH
Pick the true statement from the following.
A
I and II are preorder and inorder sequences, respectively
B
I and III are preorder and postorder sequences, respectively
C
II is the inorder sequence, but nothing more can be said about the other two sequences
D
II and III are the preorder and inorder sequences, respectively
Binary Trees    Tree Traversals    Gate IT 2008    
Discuss it


Question 23 Explanation: 
The approach to solve this question is to first find 2 sequences whose first and last element is same. The reason being first element in the Pre-order of any binary tree is the root and last element in the Post-order of any binary tree is the root. Looking at the sequences given, Pre-order   =   KAMCBYPFH Post-order  =  MBCAFHPYK Left-over sequence  MABCKYFPH will be in order. Since we have all the traversals identified, let's try to draw the binary tree if possible. pranjul I. Post order
II. Pre order
III. Inorder This solution is contributed by Pranjul Ahuja.
Question 24
Consider the following sequence of nodes for the undirected graph given below.
a b e f d g c
a b e f c g d
a d g e b c f
a d b c g e f
A Depth First Search (DFS) is started at node a. The nodes are listed in the order they are first visited. Which all of the above is (are) possible output(s)?
2008_46
A
1 and 3 only
B
2 and 3 only
C
2, 3 and 4 only
D
1, 2, and 3
Tree Traversals    Gate IT 2008    
Discuss it


Question 24 Explanation: 
1: abef->c or g should be covered
4: adbc->e or f should be covered
2: abefcgd correct
3: adgebcf correct
There are 24 questions to complete.

GATE CS Corner


See Placement Course for placement preparation, GATE Corner for GATE CS Preparation and Quiz Corner for all Quizzes on GeeksQuiz.