Tree Questions with Answers

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

Sample questions

Question 1Medium

'......' Is a directed tree in which outdegree of each node is less than or equal to two.

  1. Unary tree
  2. Binary tree
  3. Dinary tree
  4. Both B and C

Answer: B. Binary tree

Explanation

Binary tree Is a directed tree in which outdegree of each node is less than or equal to two.

Step-by-step solution

  1. The question defines a directed tree where each node has out degree at most two.
  2. That is the definition of a binary tree, option B.
  3. Option D also needs option C (Dinary) to match, and the question gives no such definition for it.
  4. So the correct answer is option B, Binary tree.

Why the other options are wrong

  • A. A unary tree is not the tree with out degree at most two.
  • C. Dinary is not the name matched by the question's definition; binary is.
  • D. It depends on option C matching the definition, which the question does not show.

Tip: Out degree at most 2 = binary tree; out degree at most d = d-ary tree.

Question 2Medium

A '_' is an acyclic digraph, which has only one node with indegree 0, and other nodes have indegree 1.

  1. Directed tree
  2. Undirected tree
  3. Dis-joint tree
  4. Direction oriented tree

Answer: A. Directed tree

Explanation

A directed graph, or digraph, is a graph in which the edges connect vertices in a specific direction.
A digraph differs from a tree in that -
it need not have a root node.
there may be several (or no) paths from one vertex to another.
it is processed differently. Take insertion for example. To add a node to a tree, a link field must be added to a parent node. For a directed graph, a node may be inserted, but there need not be an arc to or from it; or an edge can be inserted between two existing nodes.

Step-by-step solution

  1. A tree has one root with indegree 0.
  2. All other nodes have indegree 1.
  3. This matches the definition of a directed tree.
  4. Directed tree is an acyclic digraph.
  5. Final answer: Directed tree.

Why the other options are wrong

  • B. Undirected tree does not have direction.
  • C. Disjoint tree refers to separate trees, not structure.
  • D. Direction oriented tree is not a standard term.

Tip: Remember: Directed tree has one root and no cycles.

Question 3Medium

The number of possible ordered trees with 3 nodes A, B, C is

  1. 16
  2. 12
  3. 6
  4. 10

Answer: B. 12

Explanation

It is 12. The tree may be of depth 2 or 1. if the depth is 2, we have 6 possible trees. This is because one of the three nodes A, B, C may be the root and the next level may be one of the remaining two nodes. If the depth is 1, the root may be one of the 3 nodes A, B, C. Corresponding to a root, say A, two trees as possible as this.

Step-by-step solution

  1. In an ordered tree the order of children matters.
  2. Chain shape (depth 2): choose the root, then the middle node; the last node is the leaf. That is 3 × 2 = 6 ways.
  3. Root with two ordered children: choose the root, then order the other two. That is 3 × 2 = 6 ways.
  4. Total is 6 + 6 = 12: option B.

Why the other options are wrong

  • A. 16 overcounts; the valid number of ordered trees is 12.
  • C. 6 counts only the chain shape and misses the root with two children.
  • D. 10 misses some ordered shapes; the correct total is 12.

Tip: Labelled ordered trees on 3 nodes = 2 shapes × 3! labellings = 12.

Question 4Medium

The number of possible binary trees with 4 nodes is

  1. 12
  2. 13
  3. 14
  4. 15

Answer: C. 14

Explanation

Eight possible trees of depth 3. Six possible trees of depth 2. Altogether 14.

Step-by-step solution

  1. The number of distinct binary trees with n nodes is the Catalan number, C(n) = (2n)! ÷ ((n+1)! × n!).
  2. For n = 4: 8! ÷ (5! × 4!) = 40320 ÷ (120 × 24) = 40320 ÷ 2880 = 14.
  3. So there are 14 different binary tree shapes with 4 nodes.
  4. The correct answer is option C, 14.

Why the other options are wrong

  • A. 12 is below the count of 14.
  • B. 13 is one short of the count of 14.
  • D. 15 is one more than the count of 14.

Tip: Binary tree shapes with n nodes follow the Catalan numbers: 1, 2, 5, 14 for n = 1 to 4.

Question 5Medium

When representing any algebraic expression E which uses only binary operations in a 2-tree,

  1. the variable in E will appear as external nodes and operations in internal nodes
  2. the operations in E will appear as external nodes and variables in internal nodes
  3. the variables and operations in E will appear only in internal nodes
  4. the variables and operations in E will appear only in external nodes

Answer: A. the variable in E will appear as external nodes and operations in internal nodes

Explanation

Consider any algebraic expression E involving only binary operations, such as

E = (a-b)/((c*d)-1-e)

E can be represented by means of the binary tree T.That is, each variable or constant in E appears as an "internal" node in T whose left and right subtrees correspond to the operands of the operation.

Step-by-step solution

  1. Binary operations require two operands, so they must be internal nodes.
  2. Variables are operands, so they appear as external nodes.
  3. Each operation connects two external nodes.
  4. Thus, variables are external and operations are internal.
  5. Final answer: the variable in E will appear as external nodes and operations in internal nodes.

Why the other options are wrong

  • B. Operations should be internal, not external.
  • C. Both variables and operations cannot be only internal.
  • D. Both cannot be only external as operations need operands.

Question 6Medium

A binary tree T has n leaf nodes. The number of nodes of degree 2 in T is

  1. log2n
  2. n - 1
  3. n
  4. 2n

Answer: B. n - 1

Explanation

Total no. of nodes in the tree n = n0 + n1 + n2
Where ni is the number of nodes with i children.
Total no. of edges in the tree e = n0*0 + n1*1 + n2*2
But e = n - 1
n1 + 2*n2 = n0 + n1 + n2 - 1
n2 = n0 - 1
n2 = n - 1

Step-by-step solution

  1. Total nodes = leaf nodes + degree 1 nodes + degree 2 nodes.
  2. Edges in tree = total nodes - 1 = n0 + n1 + n2 - 1.
  3. Edges also equal to sum of children: n1 + 2*n2.
  4. Equating both edge counts: n1 + 2*n2 = n0 + n1 + n2 - 1.
  5. Simplify to get n2 = n0 - 1.
  6. Since leaf nodes (n0) = n, then n2 = n - 1.

Why the other options are wrong

  • A. Logarithmic relation not valid for binary tree node counts.
  • C. Leaf nodes are not equal to degree 2 nodes in a binary tree.
  • D. Double counting leaf nodes is incorrect logic.

Tip: Remember: degree 2 nodes = leaf nodes - 1 in any binary tree.

Question 7Medium

A binary tree in which every non-leaf node has non-empty left and right subtrees is called a strictly binary tree. Such a tree with 10 leaves

  1. cannot have more than 19 nodes
  2. has exactly 19 nodes
  3. has exactly 17 nodes
  4. cannot have more than 17 nodes

Answer: B. has exactly 19 nodes

Explanation

A strictly binary tree with 'n' leaves must have (2n-1) nodes. Verify for some small 'n'.

Step-by-step solution

  1. Strictly binary trees have every non-leaf node with two children.
  2. Formula: total nodes = 2 * leaves - 1.
  3. Leaves = 10, so total nodes = 2 * 10 - 1 = 19.
  4. Thus, the tree has exactly 19 nodes.

Why the other options are wrong

  • A. Cannot have more than 19 nodes is wrong as it has exactly 19.
  • C. 17 nodes would mean only 9 leaves, not 10.
  • D. Cannot have more than 17 nodes is wrong as it has exactly 19.

Tip: Remember: strictly binary trees follow total nodes = 2n - 1 where n is leaves.

Question 8Medium

The number of nodes a null tree can have is

  1. One
  2. Three
  3. Zero
  4. Null tree is invalid tree.

Answer: C. Zero

Explanation

A null tree, also known as an empty tree, has no nodes. Therefore, the number of nodes in a null tree is zero.

Step-by-step solution

  1. A null tree means there are no nodes.
  2. Therefore, the count of nodes is 0.
  3. The correct answer is Zero.

Why the other options are wrong

  • A. One node contradicts the definition of a null tree.
  • B. Three nodes is not possible in a null tree.
  • D. Null tree is valid and has zero nodes.

Tip: Remember: Null tree = zero nodes.

Question 9Medium

State true or false.
i) An empty tree is also a binary tree.
ii) In strictly binary tree, the outdegree of every node is either o or 2.

  1. True, False
  2. False, True
  3. True, True
  4. False, False

Answer: C. True, True

Explanation

An empty tree is considered a binary tree by definition because it satisfies all the properties of a binary tree, including having at most two children per node. In a strictly binary tree, every node has either zero or two children, which matches the given statement perfectly.

Step-by-step solution

  1. Statement i: an empty tree is a binary tree by definition, so statement i is true.
  2. Statement ii: in a strictly binary tree every node has 0 or 2 children, so its out degree is 0 or 2, which is true.
  3. Both statements are true, so the pair is True, True and the correct answer is C (True, True).

Why the other options are wrong

  • A. Statement ii is true, so 'False' for it is wrong.
  • B. Statement i is true, so 'False' for it is wrong.
  • D. Both statements are true, so 'False, False' is wrong.

Tip: Strictly binary tree: every node has 0 or 2 children; the empty tree counts as a binary tree.

Question 10Medium

A complete n-ary tree is one in which every node has 0 or n sons. If x is the number of internal nodes of a complete n-ary tree, the number of leaves in it is given by,

  1. x (n-1) + 1
  2. xn-1
  3. xn + 1
  4. x (n + 1)

Answer: A. x (n-1) + 1

Explanation

Degree of internal nodes (except root) = n + 1
Degree of root = n
Degree of leaf nodes = 1
Also No of edges = no. of nodes 1
But n = x + L Thus, no. of edges = x + L -1
From graph theory: sum of degree = 2 * (no. of edges)
Case 1: the tree has only 1 node.
Sum of degree
= 2 * (no of edges)
= 0
Case 2: The tree has more than one node.
(n + 1) * (x - 1) + n + L
= x + L -1

Step-by-step solution

  1. Let x be the internal nodes and L the leaves; each internal node has n children.
  2. Total nodes = x + L, so total edges = x + L - 1.
  3. Edges also equal n × x, since every internal node has n children.
  4. So x + L - 1 = nx, which gives L = x(n - 1) + 1: option A.

Why the other options are wrong

  • B. Xn - 1 ignores the leaves and is not the leaf count.
  • C. Xn + 1 is wrong; the leaf count is x(n - 1) + 1.
  • D. x(n + 1) gives too many leaves.

Tip: Leaves = internal nodes × (n - 1) + 1. Check with n = 2: one root gives 2 leaves.

Practise all 22 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