Trees & BST Questions with Answers

74 practice questions on Trees & BST (Data Structures & Algorithms) with answers and step-by-step solutions. Try 10 samples below, then practise all 74 free in the AptiNest app.

Sample questions

Question 1Easy

Which traversal of an expression tree, with parentheses added around each subexpression, gives the usual infix form?

  1. Level order
  2. Postorder
  3. Inorder
  4. Preorder

Answer: C. Inorder

Explanation

Inorder visits left operand, operator, right operand.

Step-by-step solution

  1. Inorder visits left operand, operator, right operand.
  2. That is how infix expressions are written, e.g. (3 + 4).
  3. Adding brackets around each subtree keeps the precedence correct.

Why the other options are wrong

  • A. Level order mixes operands from different subexpressions.
  • B. Postorder gives postfix (reverse Polish) notation.
  • D. Preorder gives prefix (Polish) notation.

Question 2Easy

What is the inorder traversal of this binary tree?

    A
   / \
  B   C
 / \   \
D   E   F
   /   / \
  G   H   I
  1. D B G E A C H F I
  2. D G E B H I F C A
  3. A B C D E F G H I
  4. A B D E G C F H I

Answer: A. D B G E A C H F I

Explanation

Inorder is left, root, right: D B G E A C H F I.

Step-by-step solution

  1. Inorder = left, root, right.
  2. Left of A: D B G E (G is E's left child).
  3. Right: C H F I (C has only a right child F). Result: D B G E A C H F I.

Why the other options are wrong

  • B. That is postorder.
  • C. That is level order.
  • D. That is preorder.

Question 3Easy

Keys 50, 20, 10, 30, 70, 25, 60, 5, 80, 35 are inserted in that order into an empty BST. How many leaf nodes does it have?

  1. 7
  2. 6
  3. 5
  4. 4

Answer: C. 5

Explanation

Drawing the tree, the nodes with no children are the 5 leaves.

Step-by-step solution

  1. Tree: 50 (20 (10 (5), 30 (25, 35)), 70 (60, 80)).
  2. Leaves: 5, 25, 35, 60, 80.
  3. Count = 5.

Why the other options are wrong

  • A. Counts internal nodes too.
  • B. Counts 10, which has child 5.
  • D. Misses one leaf.

Tip: Leaves = full nodes + 1.

Question 4Easy

A complete binary tree has 20 nodes. How many of them are leaves?

  1. 9
  2. 16
  3. 11
  4. 10

Answer: D. 10

Explanation

In a complete binary tree (array form), nodes with index >= floor(n/2) (0-indexed) have no children, so there are ceil(n/2) = 10 leaves.

Step-by-step solution

  1. In a complete binary tree, leaves = ceil(n / 2).
  2. ceil(20 / 2) = 10.
  3. Nodes 11 to 20 (1-based) are leaves.

Why the other options are wrong

  • A. Off by one.
  • B. 16 is the next power of 2, unrelated here.
  • C. Off by one.

Tip: Leaves in a heap of n nodes = n - floor(n/2).

Question 5Medium

What does this code print?

class Node:
    def __init__(self, key, left=None, right=None):
        self.key, self.left, self.right = key, left, right

#        1
#      /   \
#     2     3
#    / \     \
#   4   5     6
#      /     / \
#     7     8   9
root = Node(1, Node(2, Node(4), Node(5, Node(7))), Node(3, None, Node(6, Node(8), Node(9))))

def f(n, d):
    if n is None:
        return 0
    if d == 0:
        return 1
    return f(n.left, d - 1) + f(n.right, d - 1)

print(f(root, 2), f(root, 3))
  1. 3 3
  2. 3 2
  3. 2 3
  4. 4 3

Answer: A. 3 3

Explanation

Level 2 has 4, 5, 6 (3 nodes) and level 3 has 7, 8, 9 (3 nodes).

Step-by-step solution

  1. f(n, d) counts nodes exactly d levels below n.
  2. Level 2 (root at level 0): 4, 5, 6 = 3 nodes.
  3. Level 3: 7, 8, 9 = 3 nodes.

Why the other options are wrong

  • B. Misses one of 7, 8, 9 at level 3.
  • C. Misses one of 4, 5, 6 at level 2.
  • D. Counts a null slot as a node at level 2.

Question 6Medium

In a red-black tree with n keys, the height is at most:

  1. n / 2
  2. log2 n
  3. 2 log2(n + 1)
  4. sqrt(n)

Answer: C. 2 log2(n + 1)

Explanation

Every root-to-leaf path has the same number of black nodes and no two reds are adjacent, which bounds the height by 2 log2(n + 1).

Step-by-step solution

  1. Red-black rules: no two reds in a row; equal black height on all paths.
  2. So the longest path is at most twice the shortest.
  3. Height <= 2 log2(n + 1).

Why the other options are wrong

  • A. Far too large; RB trees are balanced.
  • B. That is the bound for a perfect tree, which RB trees need not be.
  • D. Not the RB bound.

Tip: RB: longest path <= 2 * shortest path.

Question 7Medium

Why are B+ trees preferred over BSTs for database indexes on disk?

  1. They store keys unsorted for faster inserts
  2. They use less memory than arrays
  3. They never need rebalancing
  4. High fan-out keeps the tree shallow, so few disk blocks are read

Answer: D. High fan-out keeps the tree shallow, so few disk blocks are read

Explanation

Each node fills a disk page with many keys, so a lookup touches only about log base m of n pages.

Step-by-step solution

  1. Disk reads are slow; each node read costs one block read.
  2. B+ tree nodes hold hundreds of keys, so the height is tiny (3 or 4 levels).
  3. Leaves are linked, making range scans fast.

Why the other options are wrong

  • A. B+ tree keys are sorted.
  • B. Memory use is not the main reason.
  • C. They rebalance by splitting and merging nodes.

Question 8Medium

What does this code print?

class Node:
    def __init__(self, key, left=None, right=None):
        self.key, self.left, self.right = key, left, right

#        1
#      /   \
#     2     3
#    / \     \
#   4   5     6
#      /     / \
#     7     8   9
root = Node(1, Node(2, Node(4), Node(5, Node(7))), Node(3, None, Node(6, Node(8), Node(9))))

out = []
def f(n, d):
    if n is None:
        return
    if d == len(out):
        out.append(n.key)
    f(n.right, d + 1)
    f(n.left, d + 1)

f(root, 0)
print(out)
  1. [1, 3, 6, 8]
  2. [1, 3, 6, 9]
  3. [1, 2, 4, 7]
  4. [1, 3, 6]

Answer: B. [1, 3, 6, 9]

Explanation

Visiting right children first and recording the first node seen at each depth gives the right view: 1, 3, 6, 9.

Step-by-step solution

  1. Visit right before left, recording the first node at each new depth.
  2. Depth 0: 1. Depth 1: 3. Depth 2: 6.
  3. Depth 3: 6's right child 9 is reached before 8 and 7.
  4. Output [1, 3, 6, 9]: the right view.

Why the other options are wrong

  • A. 8 is the left child of 6; 9 is seen first.
  • C. That is the left view.
  • D. Misses the deepest level.

Tip: Right view: DFS right-first, record when depth == len(result).

Question 9Hard

In a tree where every internal node has exactly 3 children, there are 10 internal nodes. How many leaves are there?

  1. 30
  2. 20
  3. 21
  4. 31

Answer: C. 21

Explanation

For a full k-ary tree,
L = (k - 1)I + 1
= 2 * 10 + 1 = 21.

Step-by-step solution

  1. For a full m-ary tree, L = (m - 1) * I + 1.
  2. (3 - 1) * 10 + 1 = 21.
  3. So 21 leaves.

Why the other options are wrong

  • A. 30 counts children, not leaves.
  • B. Off by one.
  • D. Counts children plus one.

Tip: m-ary full tree: L = (m - 1)I + 1.

Question 10Hard

How many different binary trees (labelled, so node identity matters) can be formed with 3 distinct nodes?

  1. 15
  2. 6
  3. 30
  4. 5

Answer: C. 30

Explanation

There are C(3) = 5 shapes, and each can be labelled in 3! = 6 ways:
5 * 6 = 30.

Step-by-step solution

  1. Unlabelled shapes with 3 nodes: C(3) = 5.
  2. Each shape can be labelled in 3! = 6 ways.
  3. 5 * 6 = 30.

Why the other options are wrong

  • A. 15 has no matching formula.
  • B. 6 counts labellings of only one shape.
  • D. 5 counts unlabelled shapes only.

Tip: Labelled trees = Catalan * n!; BSTs = Catalan.

Practise all 74 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Data Structures & Algorithms topics · All topics