GATE / CS ARCHIVE

SUBJECT COLLECTION

Data Structures · Tree Properties & Traversals

40 questions, newest first

GATE CSE has asked 40 questions on Tree Properties & Traversals (Data Structures) from 1991 to 2026, in 28 of the 46 papers; the most recent is in GATE CSE 2026 Set 2. In the last five years it carried 0.4 marks per 100-mark paper on average, against 1 since 2003. Since 2014 the questions have been 9 NAT and 6 MCQ. 6 groups of questions here are solved the same way; “One per method” keeps the newest of each.

Browse topics
1 mark · NAT
Report issue

Maximum height of a full binary tree

Tree Properties & Traversals

Question 33, GATE 2026: A full binary tree has 23 nodes. Height is the number of edges on its longest root-to-leaf path. Find the largest possible height.
Tap to enlarge
Answer & solution

Official answer: 11

Givens and goal

A full binary tree has 23 nodes. Height is the number of edges on its longest root-to-leaf path. Find the largest possible height.

Approach

Count internal nodes using the full-tree property, then arrange them along one deepest branch.

Step-by-step solution

1. “Full” means every node has either zero children or exactly two children; it does not mean balanced or complete. Let I be the number of internal nodes and L the number of leaves.
2. Every internal node contributes two child edges, so the total edge count is 2I. Any tree with I+L nodes has I+L−1 edges. Equating these gives L=I+1 and total nodes 2I+1.
3. With 23=2I+1, there are I=11 internal nodes and L=12 leaves.
4. A root-to-leaf path of h edges has h nonleaf nodes before the final leaf. All h are internal, so h≤I=11.
5. This bound is achievable by a comb: each of the first ten internal nodes has one leaf child and one internal child continuing the path; the eleventh has two leaf children. There are 11 internal nodes, 12 leaves and a deepest path of 11 edges. Therefore the maximum height is 11, not the roughly logarithmic height of a balanced tree.

Original paper, page 23 · Answer key

1 mark · MCQ
Report issue

Recognizing binary-tree traversal order

Tree Properties & Traversals

Question 12, GATE 2026: Match I Inorder, II Preorder, III Postorder to L left-node-right, M node-left-right, N left-right-node. Options: A I-L,II-M,III-N; B I-M,II-L,III-N; C I-N,II-M,III-L; D I-L,II-N,III-M.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Givens and goal

Match I Inorder, II Preorder, III Postorder to L left-node-right, M node-left-right, N left-right-node. Options: A I-L,II-M,III-N; B I-M,II-L,III-N; C I-N,II-M,III-L; D I-L,II-N,III-M.

Approach

Locate the current node relative to its two recursively traversed subtrees.

Step-by-step solution

1. In an inorder traversal the current node is visited between its left and right subtrees: left, node, right. Therefore I matches L.
2. In preorder the current node is visited before either subtree: node, left, right. Hence II matches M. In postorder it is visited after both: left, right, node, so III matches N.
3. For a root r with left leaf l and right leaf t, these orders yield l,r,t; r,l,t; and l,t,r respectively. The same definition applies recursively at every internal node, not only at the root.
4. The combined matching is I-L, II-M, III-N, option A. In a BST, inorder also produces sorted keys, but the traversal definitions themselves apply to all binary trees.

Original paper, page 11 · Answer key

1 mark · MCQ
Report issue

Tree Properties & Traversals

Question 13, GATE 2025: A nonempty binary tree has n nodes, and every node has either zero or exactly two children. How many nodes have two children?
Options: A. (n−2)/2; B. (n−1)/2; C. n/2; D. (n+1)/2.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Let i be the number of internal nodes and l the number of leaves, so n=i+l. Count the tree edges in two ways. Every non-root node has exactly one parent, giving n−1 edges in total. On the other hand, each internal node contributes two parent-to-child edges and each leaf contributes none, giving 2i edges. Therefore 2i=n−1 and i=(n−1)/2.

This also implies l=i+1=(n+1)/2, which explains why D is a plausible but incorrect distractor: it counts leaves. Such a tree has odd n, as n=2i+1. At the boundary n=1, the root is a leaf and the formula gives zero two-child nodes, as required. This is a full/proper binary tree property; it does not require the tree to be balanced or all leaves to be at the same depth. Answer: B.

Original paper, page 12 · Answer key

1 mark · NAT
Report issue

Tree Properties & Traversals

Question 16, GATE 2021: A complete binary tree has 7 nodes. A is the first three BFS-discovered nodes from root; B is the first three DFS-discovered nodes from root. Find |A−B|.
Tap to enlarge
Answer & solution

Official answer: 1

The complete seven-node tree has three levels. BFS first visits the root and both children. DFS discovery first visits the root, one child, and one grandchild. Therefore exactly one BFS member, the other child of the root, is absent from the first three DFS members, so |A−B|=1.

Name the root r, children l,u and grandchildren l1,l2,u1,u2. BFS gives {r,l,u}. A left-first DFS gives {r,l,l1}; right-first gives {r,u,u1}. In either case the intersection has two nodes and the BFS-only set has one. The result is independent of which child is explored first.

Original paper, page 64 · Answer key

2 marks · NAT
Report issue

Expected distance between tree leaves

Tree Properties & Traversals

Question 46, GATE 2019: A binary tree T has 8 leaves and every level is full (a perfect tree). Two leaves a,b are selected uniformly and independently. Find the expected number of edges on their unique connecting path, to two decimal places.
Tap to enlarge
Answer & solution

Official answer: 4.25

1. A perfect binary tree with eight leaves has height 3 because 8=2³. All leaves are at depth 3, with the root at depth zero.
2. By symmetry, fix the first chosen leaf a. Independence means b remains uniformly distributed among all eight leaves, including a itself.
3. If b=a, there is one choice and distance 0. Its sibling is one choice at distance 2, via their parent. The two leaves in the other child subtree of the same depth-one ancestor have distance 4.
4. The remaining four leaves lie across the root, so the path goes up three edges and down three: distance 6. The counts 1+1+2+4=8 exhaust all equally likely outcomes.
5. Therefore E[d]=(1×0+1×2+2×4+4×6)/8=34/8=4.25.
6. The zero-distance case is required by independent sampling. Sampling two distinct leaves would instead condition on b≠a and produce 34/7, a different answer. Also, the question's parenthetical definition fixes a perfect tree; the broader textbook meaning 'every internal node has two children' alone would not determine these distances.

Original paper, page 14 · Answer key

1 mark · NAT
Report issue

Reconstructing a binary tree height

Tree Properties & Traversals

Question 20, GATE 2018: Postorder is 8,9,6,7,4,5,2,3,1 and inorder is 8,6,9,4,7,2,5,1,3. Height counts edges on a longest root-to-leaf path. Find the height.
Tap to enlarge
Answer & solution

Official answer: 4

Method

Postorder ends with the root, and inorder splits each subtree into left part, root, right part. Root 1 splits off right leaf 3. Its left subtree ends in 2, whose inorder split leaves right leaf 5 and left subtree rooted at 4. Continuing gives 4 with children 6 and 7, and 6 with children 8 and 9. There are five vertices but four edges on the longest path, so do not report the number of levels.

Solution

The final postorder element gives root1, with right child3 and left subtree rooted 2. In that left subtree, 5 is the right child, while 4 is the left child of 2; 4 has left child6 and right child7, and 6 has children 8, 9. A longest path is 1→2→4→6→8, four edges. Thus height 4.

Original paper, page 9 · Answer key

1 mark · NAT
Report issue

Tree Properties & Traversals

Question 11, GATE 2016: BFS starts at the root of a binary tree. Vertex t is at distance 4 from the root and is the nth visited vertex, counting the root first. Find the maximum n.
Tap to enlarge
Answer & solution

Official answer: 31.0 : 31.0

Our answer: 31

BFS visits all shallower levels first. A full binary tree has 1+2+4+8=15 nodes above level4 and at most 16 nodes on level4. Taking t last on that level gives position 31, attained by a perfect tree through level4.

The last possible position at depth d in a binary tree is Σ(k=0 to d)2k=2d+1−1. Nodes at greater depth cannot precede t in BFS. Sparse levels or placing t earlier can only reduce its ordinal, so a full tree and a last-on-level choice attain the bound.

Original paper, page 28 · Answer key

2 marks · MCQ
Report issue

Tree Properties & Traversals

Question 36, GATE 2016: New-order visits root, then right subtree, then left subtree recursively. For the expression tree of postfix “3 4 * 5 - 2 ^ 6 7 * 1 + -”, choose its New-order. A + - 1 6 7 * 2 ^ 5 - 3 4 *; B - + 1 * 6 7 ^ 2 - 5 * 3 4; C - + 1 * 7 6 ^ 2 - 5 * 4 3; D 1 7 6 * + 2 5 4 3 * - ^ -.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Step through it (15 steps)

The postfix expression gives root subtraction with left subtree((3*4)−5)2 and right subtree(6*7)+1. Root-right-left order visits −, then + 1 * 7 6, then ^ 2 − 5 * 4 3. Thus − + 1 * 7 6 ^ 2 − 5 * 4 3, C.

Build the tree with a stack: push operands; for an operator pop the right child first, then the left child. The final minus combines the two large subexpressions. Reversing child visits does not exchange the operands in the original expression; it changes only traversal order. In particular the right multiplication is visited * 7 6, excluding B.

Original paper, page 35 · Answer key

1 mark · MCQ
Report issue

Tree Properties & Traversals

Question 15, GATE 2015: Height means the length in edges of the longest root-to-leaf path. Find maximum and minimum node counts of a binary tree of height 5. A 63 and 6; B 64 and 5; C 32 and 6; D 31 and 5.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Levels run from depth 0 through 5. At most 2d nodes fit at depth d, so the maximum is the geometric sum 26−1=63. The minimum still requires one path containing five edges, hence six vertices. The number 32 counts only the possible last-level nodes, not the entire tree.

Solution: Height5 counts edges. The complete tree has 1+2+4+8+16+32=63 nodes; a single path needs 6. Thus maximum 63 and minimum 6, A.

Original paper, page 7 · Answer key

1 mark · NAT
Report issue

Tree Properties & Traversals

Question 23, GATE 2015: A binary tree has 20 leaves. How many nodes have exactly 2 children?
Tap to enlarge
Answer & solution

Official answer: 19

Let n0, n1, n2 count nodes with 0, 1, 2 children. Counting edges gives n1+2n2=n0+n1+n2−1, so n2=n0−1=19, regardless of one-child nodes.

Let L, U, B count leaves, one-child nodes, and two-child nodes. The total nodes are L+U+B, and a tree has one fewer edges. Counting edges by parents instead gives U+2B. Equating U+2B=L+U+B−1 cancels U and gives B=L−1. Hence 20 leaves imply 19 two-child nodes, regardless of how many unary nodes occur.

Original paper, page 34 · Answer key

1 mark · NAT
Report issue

Tree Properties & Traversals

Question 21, GATE 2015: A binary tree T has 200 leaf nodes. How many nodes in T have exactly two children? Nodes with exactly one child are allowed.
Tap to enlarge
Answer & solution

Official answer: 199

1. Let n0,n1,n2 count nodes with zero, one and two children. Every non-root node contributes one incoming edge, so E=n0+n1+n2−1. Counting edges from parents instead gives E=n1+2n2; subtracting cancels n1 and yields the result without assuming a full binary tree.

2. Counting tree edges gives n1+2n2=n0+n1+n2−1. Hence the number with two children is n2=n0−1=200−1=199.

Original paper, page 60 · Answer key

1 mark · NAT
Report issue

Tree Properties & Traversals

Question 12, GATE 2014: A rooted binary tree with n nodes is represented by pointers. The best upper bound for counting rooted subtrees containing exactly four nodes is O(nᵃ logᵇ n). Find a+10b.
Tap to enlarge
Answer & solution

Official answer: 1

1. Method and setup. Use one postorder traversal. When processing a node, both child-subtree sizes are already known; combine them with the current node and count it if the total is four. Store size and count in the recursion result rather than recounting descendants for every possible root.

2. Work through the result and its assumptions. A postorder traversal computes each rooted subtree size as 1+left_size+right_size and increments a counter for size 4. It does constant work per node, Θ(n)=O(n¹log⁰n), so a+10b=1.

Original paper, page 10 · Answer key

2 marks · NAT
Report issue

Tree Properties & Traversals

Question 39, GATE 2014: An expression tree has root +. Its left child is -, with children +(a,b) and -(c,d); its right child is +, with children -(e,f) and +(g,h). Every leaf a,b,c,d,e,f,g,h independently takes value 0 or 1. Find the maximum value of ((a+b)-(c-d))+((e-f)+(g+h)).
Tap to enlarge
Answer & solution

Official answer: 6

1. Set up the reasoning. Expand parentheses carefully, especially the subtraction of (c-d). This turns the tree into a linear expression with independent binary variables.

2. Derive the result. Label leaves left-to-right a,..., h. The expression expands as(a+b)−(c−d)+(e−f)+(g+h)=a+b−c+d+e−f+g+h. Set the six positive-coefficient leaves to 1 and the two negative-coefficient leaves to 0. The maximum is 6.

3. Check the interpretation. The d term is positive because it is subtracted twice. Set positive-coefficient leaves to one and negative-coefficient leaves to zero. The resulting assignment attains the coefficient upper bound, so it proves a maximum rather than only an example value.

Original paper, page 40 · Answer key

1 mark · MCQ
Report issue

Tree Properties & Traversals

Question 12, GATE 2014: An ordered rooted tree has root P with children Q,R from left to right. Q has sole child S. R has children T,U,V from left to right. U has sole child W. Find the stated inorder visit sequence. A SQPTRWUV; B SQPTUWRV; C SQPTWUVR; D SQPTRUWV.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Our answer: A under the ordered-general-tree inorder convention

Step 1: Notice that R has three children, so ordinary binary-tree inorder is not sufficient to define the task. The keyed interpretation uses the ordered-tree extension: traverse the first child, visit the node, then traverse remaining children left to right. Step 2: Under that convention Q's subtree gives S,Q; P is next. R's subtree gives T,R, then U's subtree W,U, then V. Step 3: Concatenate to obtain S,Q,P,T,R,W,U,V, option A. The source's missing convention remains a qualification rather than silently treating the drawing as binary.

Original paper, page 55 · Answer key

2 marks · MCQ
Report issue

Tree Properties & Traversals

Question 41, GATE 2014: An arbitrary tree uses leftMostChild/rightSibling pointers. Function D(t): set value=0; if t is non-NULL, set value=1 when t.leftMostChild is NULL, otherwise set value=D(t.leftMostChild); then add D(t.rightSibling); return value. Passing the root returns what? A internal-node count; B tree height; C number of nodes without right siblings; D leaf count.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Step 1: A NULL pointer returns zero. A node with no child contributes one for itself, identifying a leaf. A node with children contributes only the recursive count from its child list, not one for itself. Step 2: The rightSibling recursion adds the counts of the remaining sibling subtrees. It does not test whether a sibling exists as a property to count. Step 3: Inductively each forest call counts exactly its leaves; at the root, whose sibling pointer is NULL, this is the entire tree's leaf count, D. The recurrence adds branches rather than taking a maximum, so it does not compute height.

Original paper, page 62 · Answer key

2 marks · MCQ
Report issue

Recursive rooted height and wording mismatch

Tree Properties & Traversals

Question 47, GATE 2012: The source defines tree height as “the number of edges on the longest path in the tree” and gives this rooted-tree routine. Fill B1,B2:
int height(treeptr n) {
 if(n==NULL) return -1;
 if(n->left==NULL)
   if(n->right==NULL) return 0; else return B1;
 else { h1=height(n->left);
   if(n->right==NULL) return 1+h1;
   else { h2=height(n->right); return B2; }
 }
}
(A) B1=1+height(n->right), B2=1+max(h1,h2).
(B) B1=height(n->right), B2=1+max(h1,h2).
(C) B1=height(n->right), B2=max(h1,h2).
(D) B1=1+height(n->right), B2=max(h1,h2).
Tap to enlarge
Your answer
Answer & solution

Official answer: A

For the conventional rooted height, a leaf has height 0 and every parent-to-child step adds one edge. If only the right child exists, B1 must be 1+height(n→right). If both exist, B2 must be 1+max(h1, h2). Thus A is the intended rooted-height answer and matches the official key. However, the opening sentence literally defines the longest path anywhere in the tree, which is its diameter, not necessarily its root-to-leaf height. A root with two leaf children has rooted height 1 but diameter 2; none of the listed B2 expressions returns 2 for that example. Preserve this wording defect rather than claiming the code computes the literal definition.

Check: Check a node with only a right leaf: the child returns 0 and the parent must return 1, forcing B1’s +1. With two children, choose the larger subtree height and add the edge from the current node, forcing B2’s +1.

Original paper, page 12 · Answer key

1 mark · MCQ
Report issue

Odd subtree sizes in a binary tree

Tree Properties & Traversals

Question 10, GATE 2010: A binary tree has n nodes. Every node has an odd number of descendants, counting the node itself as its own descendant. How many nodes have exactly one child?
Options: (A) 0; (B) 1; (C) (n−1)/2; (D) n−1.
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

The descendants of a node, including itself, form its subtree. If a node had exactly one child, its subtree size would be 1 plus the child’s odd subtree size, hence even. This contradicts the condition at that node. Therefore no node has exactly one child: 0, A.

The parity condition applies to every child subtree as well as the root. A leaf has subtree size 1, which is allowed. A node with two children has size 1+odd+odd, also odd. Exactly one child gives 1+odd, even, which is impossible; thus the hypothesis forces a full binary tree, although it need not be balanced or perfect.

Original paper, page 3

1 mark · MCQ
Report issue

Maximum nodes at a given edge height

Tree Properties & Traversals

Question 12, GATE 2007: The height of a binary tree is the maximum number of edges on a root-to-leaf path. What is the maximum number of nodes at height h?
Options:
(A) 2^h−1
(B) 2^(h−1)−1
(C) 2^(h+1)−1
(D) 2^(h+1)
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

1. Approach. With edge height, the root is at depth zero. Count all levels including both level zero and level h, rather than counting only h levels.

2. Derivation and interpretation. At depth i a binary tree has at most 2i nodes. For edge height h, depths 0 through h sum to 1+2+...+2h=2h+1−1, C, attained by a perfect binary tree.

Original paper, page 3

1 mark · MCQ
Report issue

Three-node binary-tree shapes

Tree Properties & Traversals

Question 13, GATE 2007: How many different binary trees can be formed using three unlabeled nodes?
Options:
(A) 1
(B) 5
(C) 4
(D) 3
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. Approach. Distinguish left and right child positions. There are four three-node chains (LL, LR, RL, RR) and one root with two children.

2. Derivation and interpretation. For ordered binary trees, let T(0)=1 and T(n)=sum T(i)T(n−1−i). Then T(1)=1,T(2)=2 and T(3)=2+1+2=5, B. Left and right child positions are distinguished, as in the standard binary-tree convention; unlabeled does not mean unordered.

Original paper, page 3

2 marks · MCQ
Report issue

Postorder from inorder and preorder

Tree Properties & Traversals

Question 39, GATE 2007: A binary tree has inorder traversal d,b,e,a,f,c,g and preorder traversal a,b,d,e,c,f,g. What is its postorder traversal?
Options:
(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
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

1. Approach. The first preorder item is the root. Its position in inorder separates left and right subtrees; recursively repeat, then output left subtree, right subtree, root.

2. Derivation and interpretation. Preorder starts with root a. Inorder splits the left subtree as d,b,e and the right as f,c,g. The remaining preorder makes b the left root with children d,e, and c the right root with children f,g. Postorder is d,e,b,f,g,c,a, option A.

Original paper, page 9

2 marks · MCQ
Report issue

Leaves and branching factor of a full n-ary tree

Tree Properties & Traversals

Question 43, GATE 2007: Here a complete n-ary tree means each node has exactly n children or none. It has I=10 internal nodes and L=41 leaves. Find n.
Options:
(A) 3
(B) 4
(C) 5
(D) 6
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

1. Approach. Count child edges once from the parent side and once using the identity edges=vertices−1 for a tree. Equating the counts eliminates the shape of the tree.

2. Derivation and interpretation. Count the edges in two ways. Each of the I internal nodes contributes exactly n child edges, while a tree with I+L vertices has I+L−1 edges. Thus nI=I+L−1 and n=(10+41−1)/10=5 (C). The question calls this complete, but explicitly defines the full n-ary property, which is all the calculation needs.

Original paper, page 10

2 marks · MCQ
Report issue

Recursive leaf counting in a binary tree

Tree Properties & Traversals

Question 46, GATE 2007: CellNode represents a binary-tree node. What does GetValue return when called on the root?
struct CellNode {
    struct CellNode *leftChild;
    int element;
    struct CellNode *rightChild;
};
int GetValue(struct CellNode *ptr) {
    int value=0;
    if(ptr!=NULL) {
        if(ptr->leftChild==NULL && ptr->rightChild==NULL)
            value=1;
        else value=value+GetValue(ptr->leftChild)+GetValue(ptr->rightChild);
    }
    return value;
}
Options:
(A) Number of nodes
(B) Number of internal nodes
(C) Number of leaves
(D) Tree height
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

1. Approach. Check the two base cases: null contributes zero, and a node with no children contributes one. All other nodes merely add child results, so their own elements and depths are irrelevant.

2. Derivation and interpretation. GetValue returns zero for a null pointer, one when both children are null, and otherwise the sum of the values for the left and right subtrees. By induction on the tree, this counts precisely the leaves: an internal node adds no contribution of its own. Therefore C, the number of leaf nodes, is correct, including zero for an empty tree.

Original paper, page 11

1 mark · MCQ
Report issue

Worst array index for a binary tree

Tree Properties & Traversals

Question 13, GATE 2006: Store any n-node binary tree in a one-based array: root at X[1], left child of X[i] at X[2i], right child at X[2i+1]. What minimum array capacity guarantees room for every possible n-node tree?
Options:
(A) log₂n
(B) n
(C) 2n+1
(D) 2^n−1
Question 13, GATE 2006: Store any n-node binary tree in a one-based array: root at X[1], left child of X[i] at X[2i], right child at X[2i+1]. What minimum array capacity guarantees room for every possible n-node tree?
Options:
(A) log₂n
(B) n
(C) 2n+1
(D) 2^n−1
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. Following right children produces indices 1,3,7,...,2n−1 for an n-node chain. No n-node tree has depth beyond n−1, whose largest possible index is 2n−1.

2. Therefore an array indexed from 1 must provide that many positions, D. This counts logical one-based entries, not an extra unused C array index 0.

3. Teaching check: A chain of only right children is the worst case, not a balanced tree. Repeatedly applying i↦2i+1 from i=1 gives iₖ=2k+1−1 at depth k. The last of n nodes therefore occupies index 2n−1, although only n positions contain actual nodes.

Original paper, page 2

2 marks · MCQ
Report issue

Leaves in a full k-ary tree

Tree Properties & Traversals

Question 36, GATE 2005: A finite full k-ary tree has n internal nodes, each with exactly k children. Number of leaves? A nk; B(n−1)k+1; C n(k−1)+1; D n(k−1).
Question 36, GATE 2005: A finite full k-ary tree has n internal nodes, each with exactly k children. Number of leaves? A nk; B(n−1)k+1; C n(k−1)+1; D n(k−1).
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

If there are n internal nodes and L leaves, the number of parent-child edges is kn. Any finite tree also has n+L−1 edges. Equating gives L=n(k−1)+1, C. The premise is that every internal node has exactly k children, not merely at most k.

Count the same edges in two ways. Every internal node contributes k outgoing parent-child edges, for kn. Any tree on n+L total nodes has n+L−1 edges. Equating and solving yields L=kn−n+1=n(k−1)+1. This method does not require every leaf to have the same depth; the exact-k-child premise is what matters.

Original paper, page 4

1 mark · MCQ
Report issue

Level-order traversal

Tree Properties & Traversals

Question 6, GATE 2004: Starting at the root, which traversal produces level order in a rooted tree?
Options:
(A) Preorder
(B) Inorder
(C) Depth-first search
(D) Breadth-first search
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. Breadth-first search uses a queue to visit the root, then all children, then their children, and so on. This is precisely level order, D; depth-first traversals do not generally preserve depth order.

2. Teaching check: Begin with just the root in a queue. Repeatedly dequeue a vertex and enqueue its children. Children of a depth-d vertex are enqueued behind all remaining depth-d vertices, so no depth-(d+1) vertex is processed prematurely.

Original paper, page 1

2 marks · MCQ
Report issue

Traversal pairs uniquely determining a binary tree

Tree Properties & Traversals

Question 35, GATE 2004: For an unrestricted binary tree with distinct node labels, which pairs of label sequences determine the tree uniquely? (i) preorder and postorder; (ii) inorder and postorder; (iii) preorder and inorder; (iv) level order and postorder.
Options:
(A) i only
(B) ii and iii
(C) iii only
(D) iv only
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. With distinct node labels, inorder plus preorder identifies the root then splits both subtrees recursively; inorder plus postorder does the same using the final element as root. Thus ii and iii, B.

2. A root with one left child versus one right child has identical preorder, postorder and level order, so the other pairs do not suffice for unrestricted binary trees. Distinct labels are the conventional assumption.

3. Teaching check: For a concrete ambiguity, a root A with sole left child B and a root A with sole right child B both have preorder A,B, postorder B,A and level order A,B. Inorder distinguishes B,A from A,B, explaining why it is indispensable among the offered pairs.

Original paper, page 5

2 marks · MCQ
Report issue

Recursive binary-tree height

Tree Properties & Traversals

Question 43, GATE 2004: Consider a binary-tree node with fields leftChild, element and rightChild, and this function (max returns the larger argument):
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;
}
What is returned when ptr is the root of a nonempty tree?
Options:
(A) Number of leaf nodes
(B) Number of nodes
(C) Number of internal nodes
(D) Height of tree
Question 43, GATE 2004: Consider a binary-tree node with fields leftChild, element and rightChild, and this function (max returns the larger argument):
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;
}
What is returned when ptr is the root of a nonempty tree?
Options:
(A) Number of leaf nodes
(B) Number of nodes
(C) Number of internal nodes
(D) Height of tree
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. A leaf returns zero. A node with a child takes one plus that child’s recursively computed value; with two children it takes the maximum.

2. Induction gives the maximum number of edges from the node to a descendant leaf, i.e. height measured in edges. The null check returns zero but no missing child adds an edge.

3. The code does not count nodes or leaves.

4. Teaching check: A singleton tree returns zero, immediately excluding both total-node and leaf counts. A two-edge chain returns two. The recursive maximum chooses the deepest child path, while adding one accounts for the edge from the current node to that child.

Original paper, page 6

Descriptive
Report issue

Enumerate binary trees with fixed preorder

Tree Properties & Traversals

Question 6, GATE 2002: Draw every binary tree with exactly three labeled nodes A, B, C for which preorder traversal visits C, B, A in that order.
Tap to enlarge
Shared context

Find every ordered binary tree containing exactly the distinct labels A, B, C whose preorder sequence is C,B,A. Left and right child positions are distinguished.

Answer & solution

Answer: Exactly 5 ordered binary trees (no official key was published)

1. Use the first preorder symbol to fix the root. Preorder visits root, then left subtree, then right subtree. Therefore every requested tree must have C at its root.

2. Partition the two remaining nodes between C’s subtrees. The possible left/right subtree sizes are (2,0), (0,2), and (1,1). These cases exhaust the possibilities because there are exactly two nodes besides C.

3. Handle the two single-subtree cases. For sizes (2,0), B is C’s left child, and A is either B’s left child or B’s right child: two trees. For sizes (0,2), B is C’s right child, and A again has two child positions: two more trees. B must precede A because their subtree’s preorder must be B,A.

4. Handle sizes (1,1). Preorder visits the entire left subtree before the right, so the left child is B and the right child is A: one tree. Reversing those children would give C,A,B and is not allowed.

5. Draw the resulting 2 + 2 + 1 = 5 ordered binary trees:

(1)      C         (2)      C
        /                 /
       B                 B
      /                   \
     A                     A

(3)      C         (4)      C
          \                 \
           B                 B
          /                   \
         A                     A

(5)      C
        / \
       B   A

6. Verify each tree by reading root-left-right. Each gives C,B,A. The exhaustive subtree-size split proves that no additional ordered binary-tree shape can qualify. In explicit node(left,right) notation, with - for no child, the five trees are C(B(A(-,-),-),-), C(B(-,A(-,-)),-), C(-,B(A(-,-),-)), C(-,B(-,A(-,-))), and C(B(-,-),A(-,-)).

Original paper, page 7

Descriptive
Report issue

Complete recursive tree depth

Tree Properties & Traversals

Question 14(c), GATE 2001: depth(t) returns 0 for NULL, sets x=depth(t->left), then needs S1, S2 and S3 to return the tree depth.
Question 14(c), GATE 2001: depth(t) returns 0 for NULL, sets x=depth(t->left), then needs S1, S2 and S3 to return the tree depth.
Tap to enlarge
Answer & solution

Answer: S1: y=depth(t->right); S2: x+1; S3: y+1. (no official key was published)

The null-tree base case fixes depth as the number of nodes on a longest root-to-leaf path. Each nonempty tree contributes its root plus the larger child depth. Full compilable function:

#include <stddef.h>
typedef struct node {
    int key;
    struct node *left, *right;
} *Tree;
int depth(Tree t) {
    int x, y;
    if (t == NULL) return 0;
    x = depth(t->left);
    y = depth(t->right); /* S1 */
    if (x > y) return x + 1; /* S2 */
    else return y + 1; /* S3 */
}

A leaf returns 1. The preceding tree returns 4. Each node is visited once, giving O(n) time and O(h) recursive stack space; an edge-count definition would require a different base case and is not the convention of the supplied program.

Original paper, page 9

1 mark · MCQ
Report issue

Well-formed nested binary-tree notation

Tree Properties & Traversals

Question 1.14, GATE 2000: A nonleaf is written (root left right), with leaves represented by labels and empty subtrees by NULL. Choose a valid expression: A (1 2 (4 5 6 7)); B (1 ((2 3 4) 5 6) 7); C (1 (2 3 4) (5 6 7)); D (1 (2 3 NULL) (4 5)).
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

Step 1: Every parenthesized node must begin with a node label and have exactly two child descriptions. C has root 1, left subtree rooted 2 with leaves 3,4, and right subtree rooted 5 with leaves 6,7.

Step 2: A gives node 4 three children. B uses a whole subtree where a root label should be. D gives node 4 only one child without an explicit second subtree.

Only C consistently follows the notation.

Original paper, page 2

2 marks · MCQ
Report issue

Last visited vertices in binary-tree traversals

Tree Properties & Traversals

Question 2.16, GATE 2000: For a “complete binary tree,” compare the last vertices in postorder,inorder,preorder. Options equate inorder/postorder,inorder/preorder,preorder/postorder,or none.
Tap to enlarge
Your answer
Answer & solution

Answer: D for heap-complete trees; B for perfect/full-rightmost-path convention (no official key was published)

Step 1: Postorder ends at the root. Inorder ends at the rightmost node, while preorder ends at the last leaf visited after exploring subtrees.

Step 2: Under the modern complete-tree definition (last level filled left to right), take six heap-indexed nodes. Node 3 has left child 6 but no right child. Inorder ends at 3, preorder at 6, and postorder at 1. All three differ, so none of the proposed equalities is always true: D.

If “complete” is intended to mean perfect, the rightmost node is a leaf and is last in both inorder and preorder, giving B. The terminology ambiguity is retained rather than silently imposing the stronger shape.

Original paper, page 5

3 marks · Descriptive
Report issue

Leaves equal full nodes plus one

Tree Properties & Traversals

Question 12(a), GATE 1999: In a nonempty binary tree, a full node has exactly two children. Prove by induction on height that the number of leaves equals the number of full nodes plus one.
Question 12(a), GATE 1999: In a nonempty binary tree, a full node has exactly two children. Prove by induction on height that the number of leaves equals the number of full nodes plus one.
Tap to enlarge
Shared context

In a nonempty binary tree, a full node has exactly two children. Prove by induction on height that the number of leaves equals the number of full nodes plus one.

Answer & solution

Answer: For every nonempty binary tree, L=F+1, including trees with one-child nodes. (no official key was published)

Use strong induction on the height h, taking a leaf to have height zero. Let L(T) and F(T) count leaves and full nodes in T.

Base h=0: T is a single leaf, so L=1 and F=0, giving L=F+1.

Induction hypothesis: the identity holds for every nonempty binary tree of height less than h. Consider a tree T of height h>0. Its root has one or two children.

If it has one child, the unique child subtree T₁ has smaller height. The root is neither full nor a leaf, so L(T)=L(T₁) and F(T)=F(T₁). By induction L(T)=F(T₁)+1=F(T)+1.

If it has two children with subtrees T₁,T₂, both have smaller height. Here L(T)=L(T₁)+L(T₂) and F(T)=1+F(T₁)+F(T₂), because the root itself is full. Substituting the two induction hypotheses gives L(T)=F(T₁)+1+F(T₂)+1=F(T)+1.

These cases exhaust all roots, proving the claim. The qualification “nonempty” matters: an empty tree has zero leaves and zero full nodes, so the formula would not hold there. It is unnecessary to assume every internal node is full.

Original paper, page 7

1 mark · MCQ
Report issue

Tree facts and traversal uniqueness

Tree Properties & Traversals

Question 1.24, GATE 1998: Which is false: n-node trees have n−1 edges; preorder plus postorder uniquely determine a labelled binary tree; full binary trees have leaves=internal+1; maximum nodes at edge-height h is 2^(h+1)−1?
Tap to enlarge
Your answer
Answer & solution

Answer: B under the full-tree interpretation of complete (no official key was published)

Take a root r with only one child c. Making c the left child or the right child gives the same preorder r, c and postorder c, r, so uniqueness fails: B. A connected acyclic n-node graph has n−1 edges. A full binary tree satisfies 2I=I+L−1, hence L=I+1. Summing complete levels 0 through h gives 2h+1−1. The source says “complete” for the leaf formula; modern heap-complete trees may have a one-child node, invalidating that formula. Under that terminology C can fail too, so the intended full/proper definition is a qualification.

Original paper, page 3

5 marks · Descriptive
Report issue

Reconstruct traversal tree

Tree Properties & Traversals

Question 20, GATE 1998: Construct the tree with inorder a f b c d g e and postorder a f c g e d b.
Question 20, GATE 1998: Construct the tree with inorder a f b c d g e and postorder a f c g e d b.
Tap to enlarge
Answer & solution

Answer: Root b; left f with left a; right d with left c and right e with left g. (no official key was published)

The final postorder symbol b is the root. Split inorder around b: left subtree has a,f and right subtree has c,d,g,e. The corresponding postorder chunks are a,f and c,g,e,d.

For the left subtree, final f is its root and a lies to its left, so f has left child a and no right child. For the right subtree, final d is its root. Inorder splits into c on its left and g,e on its right. The left child is c. The right chunk has postorder g,e, hence root e with left child g.

        b
       / \
      f   d
     /   / \
    a   c   e
           /
          g

Checking inorder yields a,f,b,c,d,g,e. Checking postorder yields a,f,c,g,e,d,b. The labels are distinct, so the recursive splits uniquely determine the tree. A different shape that merely shares the root would not suffice.

Original paper, page 7

1 mark · MCQ
Report issue

Postorder of the pictured tree

Tree Properties & Traversals

Question 1.15, GATE 1996: For root a with left subtree b(c(g),d) and right subtree e(f), find the postorder traversal.
Question 1.15, GATE 1996: For root a with left subtree b(c(g),d) and right subtree e(f), find the postorder traversal.
Tap to enlarge
Your answer
Answer & solution

Answer: C: g,c,d,b,f,e,a (no official key was published)

Postorder visits the complete left subtree, then the complete right subtree, then the root. Within b, visit g before its parent c, then d, then b: g,c,d,b. Within e, visit f before e: f,e. Concatenate those two lists and finally append a, yielding g,c,d,b,f,e,a. The exact left-versus-right placement of a sole child does not affect postorder. Visiting a before its subtrees would be preorder, not the requested traversal.

Original paper, page 2

1 mark · MCQ
Report issue

Leaves and binary branching nodes

Tree Properties & Traversals

Question 1.17, GATE 1995: A nonempty rooted binary tree has n leaves. How many nodes have two children?
Tap to enlarge
Your answer
Answer & solution

Answer: B: n−1 (no official key was published)

Let n0,n1,n2 count nodes with zero, one and two children. The tree has n0+n1+n2−1 edges because it is connected and acyclic. Counting edges by parents gives n1+2n2. Equating the two expressions yields n2=n0−1=n−1. Unary nodes cancel and therefore do not alter the relationship. Here degree two means two children, the rooted-tree convention; interpreting it as undirected graph degree would be a different question.

Original paper, page 2

Descriptive
Report issue

Enumerate postorder binary trees

Tree Properties & Traversals

Question 6, GATE 1995: How many ordered binary trees on three nodes have postorder A,B,C? Draw all of them.
Tap to enlarge
Answer & solution

Answer: 5 (no official key was published)

Postorder visits the left subtree, then the right subtree, then the root, so C must be the root. Partition the first two labels between its subtrees. If both subtrees are nonempty, each has one node: left A and right B. If only one subtree is nonempty, that subtree has postorder A,B, so its root is B and A can be either B’s left or right child. The two-node subtree itself can be C’s left or right child, giving four more trees.

A compact complete drawing uses node(left,right), with ∅ denoting an absent child:
1. C(A,B)
2. C(B(A,∅),∅)
3. C(B(∅,A),∅)
4. C(∅,B(A,∅))
5. C(∅,B(∅,A)).
Here A or B written without parentheses is a leaf. Each drawing emits A then B then C under postorder. The cases exhaust left-subtree sizes 0,1,2, giving the Catalan count C₃=C₀C₂+C₁C₁+C₂C₀=2+1+2=5. Left and right child positions are distinguished; these are not restricted to full binary trees.

Original paper, page 6

Descriptive
Report issue

Reconstruction from preorder and postorder

Tree Properties & Traversals

Question 8, GATE 1994: A rooted tree has nodes numbered 1 to 12 in preorder. Its postorder is 3,5,4,2,7,8,6,10,11,12,9,1. Reconstruct it, give every parent, and show the tree.
Tap to enlarge
Answer & solution

Answer: Parents: 1:none; 2:1; 3:2; 4:2; 5:4; 6:1; 7:6; 8:6; 9:1; 10:9; 11:9; 12:9. (no official key was published)

1. The final postorder entry 1 is the root. The next preorder value 2 starts its first child subtree; that subtree ends at 2 in postorder, so it contains {2,3,4,5}.

2. Within subtree 2, node 3 is first and is already a complete subtree. The next child 4 has postorder 5,4 and therefore child 5.

3. The next unused preorder node is 6. Its postorder block is 7,8,6, giving children 7 and 8.

4. The remaining child subtree of 1 is rooted at 9 and has leaves 10,11,12.

5. These are ordered children according to the given traversals. A general rooted tree is intended, not a binary-tree constraint.

Diagram / structural description

1
├─2
│ ├─3
│ └─4
│   └─5
├─6
│ ├─7
│ └─8
└─9
  ├─10
  ├─11
  └─12

Original paper, page 5

2 marks · Other
Report issue

Weighted external path length

Tree Properties & Traversals

Question 1(viii), GATE 1991: Leaves of weights 2, 4, 5, 7 have depth 4; weights 9, 10 have depth 3; weight 15 has depth 1 in the source binary tree. Find its weighted external path length.
Question 1(viii), GATE 1991: Leaves of weights 2, 4, 5, 7 have depth 4; weights 9, 10 have depth 3; weight 15 has depth 1 in the source binary tree. Find its weighted external path length.
Tap to enlarge
Answer & solution

Answer: 144 (no official key was published)

External path length weights each leaf by the number of edges from the root, with root depth zero. Grouping equal depths gives 4(2+4+5+7)+3(9+10)+1(15). The sums are 4×18=72, 3×19=57, and 15, totaling 144. Internal unlabeled nodes contribute nothing to this external measure. Counting vertices instead of edges would add the sum of leaf weights, so the conventional edge-depth definition is essential. The supplied depth list is a transcription of the inspected tree, not a newly constructed tree.

Original paper, page 1

2 marks · Other
Report issue

Inorder traversal of the displayed tree

Tree Properties & Traversals

Question 1(ix), GATE 1991: Root 7 has left child 1 with children 4, 6 and right child 3 with right child 5; node 5 has children 2, 8. Give inorder traversal.
Question 1(ix), GATE 1991: Root 7 has left child 1 with children 4, 6 and right child 3 with right child 5; node 5 has children 2, 8. Give inorder traversal.
Tap to enlarge
Answer & solution

Answer: 4, 1, 6, 7, 3, 2, 5, 8 (no official key was published)

Inorder recursively visits left subtree, node, then right subtree. The subtree rooted at 1 therefore gives 4, 1, 6. Next visit root 7. Node 3 has no left child, so visit 3 immediately, then traverse its right child 5 as 2, 5, 8. Concatenating gives 4, 1, 6, 7, 3, 2, 5, 8. The numeric labels are merely node names; this is not a binary search tree, and sorting the labels would not be a traversal. Every node appears exactly once.

Original paper, page 1