Binary Tree · Data Structures
35 questions · asked in 17 of 27 years (2000–2026)
MCQ 27 · NAT 81-mark 26 · 2-mark 9
2000Questions per year2026
Questions 31–35 of 35
Consider the following C program segment
struct CellNode
{
struct CellNode *leftChild;
int element;
struct CellNode *rightChild;
}
int Dosomething(struct CellNode *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 isAnswer: (D)
For a node, the function starts with .
If the node has a left child, it sets . If it has a right child, it sets .
So a leaf returns 0, and any other node returns 1 plus the larger of its children's results. This is the length of the longest path from the node down to a leaf, counted in edges, which is the height of the tree.
Check with a complete tree of 7 nodes: it returns 2. It has 4 leaves, 3 internal nodes and 7 nodes, so the result matches none of options (A), (B) and (C).
(The paper names the function Dosomething in its definition and DoSomething in the calls; both mean the same function. The struct declaration is also printed without its closing semicolon.)
Answer: (D)
If the node has a left child, it sets . If it has a right child, it sets .
So a leaf returns 0, and any other node returns 1 plus the larger of its children's results. This is the length of the longest path from the node down to a leaf, counted in edges, which is the height of the tree.
Check with a complete tree of 7 nodes: it returns 2. It has 4 leaves, 3 internal nodes and 7 nodes, so the result matches none of options (A), (B) and (C).
(The paper names the function Dosomething in its definition and DoSomething in the calls; both mean the same function. The struct declaration is also printed without its closing semicolon.)
Answer: (D)
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
(i) preorder and postorder
(ii) inorder and postorder
(iii) preorder and inorder
(iv) level order and postorder
Answer: (D)
Assume all labels are different.
(iii) Preorder and inorder: the first label of the preorder is the root. Its place in the inorder splits the other labels into left and right subtrees. Repeat on each part. So the tree is unique.
(ii) Inorder and postorder: the last label of the postorder is the root. Its place in the inorder splits the left and right subtrees. Repeat. So the tree is unique.
(i) Preorder and postorder: not unique. Take a root A with a single child B. If B is the left child, the preorder is AB and the postorder is BA. If B is the right child, the preorder is again AB and the postorder is again BA.
(iv) Level order and postorder: not unique, for the same pair of trees: level order AB and postorder BA fit both.
Without the inorder, a lone child can be on either side, so only (ii) and (iii) work.
Answer: (D)
(iii) Preorder and inorder: the first label of the preorder is the root. Its place in the inorder splits the other labels into left and right subtrees. Repeat on each part. So the tree is unique.
(ii) Inorder and postorder: the last label of the postorder is the root. Its place in the inorder splits the left and right subtrees. Repeat. So the tree is unique.
(i) Preorder and postorder: not unique. Take a root A with a single child B. If B is the left child, the preorder is AB and the postorder is BA. If B is the right child, the preorder is again AB and the postorder is again BA.
(iv) Level order and postorder: not unique, for the same pair of trees: level order AB and postorder BA fit both.
Without the inorder, a lone child can be on either side, so only (ii) and (iii) work.
Answer: (D)
Level order traversal of a rooted tree can be done by starting from the root and performing
Answer: (D)
Level order means visiting all nodes at depth 0, then all at depth 1, then depth 2, and so on. Breadth first search does exactly this: it uses a queue, so every node at depth is visited before any node at depth .
Preorder, in-order and depth first search go deep into one branch before the next. For example, take a root a with children b and c, and a child d under b. Level order is a, b, c, d, but preorder is a, b, d, c.
Answer: (D)
Preorder, in-order and depth first search go deep into one branch before the next. For example, take a root a with children b and c, and a child d under b. Level order is a, b, c, d, but preorder is a, b, d, c.
Answer: (D)
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?
Answer: (D)
A complete binary tree has all levels full except possibly the last, which is filled from the left.
Facts: in postorder the root is always visited last, so LASTPOST is the root. In inorder the last node is found by going right from the root as long as a right child exists. In preorder the last node is found by going to the right child if there is one, else to the left child, until a leaf is reached.
Take the tree with 3 nodes: root R, left child L, right child T. Postorder is L T R, inorder is L R T, preorder is R L T. So LASTPOST = R, LASTIN = T, LASTPRE = T. Then LASTIN is not LASTPOST (A is false) and LASTPRE is not LASTPOST (C is false).
Take the tree with 2 nodes: root R with only a left child L. Inorder is L R, so LASTIN = R. Preorder is R L, so LASTPRE = L. Then LASTIN is not LASTPRE (B is false).
So none of the three equalities always holds.
Answer: (D)
Facts: in postorder the root is always visited last, so LASTPOST is the root. In inorder the last node is found by going right from the root as long as a right child exists. In preorder the last node is found by going to the right child if there is one, else to the left child, until a leaf is reached.
Take the tree with 3 nodes: root R, left child L, right child T. Postorder is L T R, inorder is L R T, preorder is R L T. So LASTPOST = R, LASTIN = T, LASTPRE = T. Then LASTIN is not LASTPOST (A is false) and LASTPRE is not LASTPOST (C is false).
Take the tree with 2 nodes: root R with only a left child L. Inorder is L R, so LASTIN = R. Preorder is R L, so LASTPRE = L. Then LASTIN is not LASTPRE (B is false).
So none of the three equalities always holds.
Answer: (D)
Consider the following nested representation of binary trees: (X Y Z) indicates Y and Z are the left and right subtrees, respectively, of node X. Note that Y and Z may be NULL, or further nested. Which of the following represents a valid binary tree?
Answer: (C)
A valid tree is written as (X Y Z): exactly three items, where X is the node and Y and Z are each NULL or another nested (X Y Z). Parentheses must be balanced.
(A) The part (4 5 6 7) has four items, so it is not of the form (X Y Z). Invalid.
(B) (1 (2 3 4) 5 6) has four items, and the string ends with 7), which has a closing parenthesis that matches nothing. Invalid.
(C) (1 (2 3 4) (5 6 7)): node 1 has left subtree (2 3 4) and right subtree (5 6 7). Each of these is a node with two items after it, and the parentheses balance. Valid.
(D) (4 5) has only two items, with no right subtree given, so it is not of the form (X Y Z). It would be valid as (4 5 NULL). Invalid.
Note: the statement says Y and Z are NULL or nested groups, so a bare label such as 3 in (2 3 4) is strictly not covered. The key reads a bare label as a leaf node.
Answer: (C)
(A) The part (4 5 6 7) has four items, so it is not of the form (X Y Z). Invalid.
(B) (1 (2 3 4) 5 6) has four items, and the string ends with 7), which has a closing parenthesis that matches nothing. Invalid.
(C) (1 (2 3 4) (5 6 7)): node 1 has left subtree (2 3 4) and right subtree (5 6 7). Each of these is a node with two items after it, and the parentheses balance. Valid.
(D) (4 5) has only two items, with no right subtree given, so it is not of the form (X Y Z). It would be valid as (4 5 NULL). Invalid.
Note: the statement says Y and Z are NULL or nested groups, so a bare label such as 3 in (2 3 4) is strictly not covered. The key reads a bare label as a leaf node.
Answer: (C)
Keep practising