If T is a binary tree with N nodes, then the number of levels is at least:

2017

If T is a binary tree with N nodes, then the number of levels is at least:

  1. A.

    ⌈log₂(N + 1)⌉

  2. B.

    N − 1

  3. C.

    N

  4. D.

    ⌊log₂(N + 1)⌋

Attempted by 404 students.

Show answer & explanation

Correct answer: A

A binary tree with L levels (root at level 1) is full when every level is completely filled; such a tree holds at most 2L − 1 nodes, since level i can hold at most 2i−1 nodes and summing from i = 1 to L gives 2L − 1. The number of levels needed for N nodes is therefore minimized only when the tree is packed as close to full as possible, and grows larger as the tree becomes more skewed.

  1. For a tree with L levels, the maximum node capacity is 2L − 1 (a full binary tree).

  2. To hold N nodes in L levels requires 2L − 1 ≥ N, i.e. 2L ≥ N + 1, i.e. L ≥ log₂(N + 1).

  3. Since L must be a whole number of levels, the smallest valid L is the ceiling of this bound: Lmin = ⌈log₂(N + 1)⌉.

Check with N = 4: ⌈log₂(5)⌉ = ⌈2.32⌉ = 3. A tree with 2 levels can hold at most 22 − 1 = 3 nodes, which is fewer than 4, so 2 levels are not enough; a tree with 3 levels can hold up to 23 − 1 = 7 nodes, which comfortably fits 4 — confirming that 3 is indeed the minimum.

So the minimum number of levels for a binary tree with N nodes is ⌈log₂(N + 1)⌉.

Explore the full course: Niacl Ao It Specialist