Data Structures & AlgorithmsModerateProduct Company SDE pattern

How many structurally different binary trees have n = 4 nodes, and how many distinct labelled binary trees can be formed from 4 distinct keys?

How many structurally different binary trees have n = 4 nodes, and how many distinct labelled binary trees can be formed from 4 distinct keys?
from math import comb, factorial
cat = lambda n: comb(2 * n, n) // (n + 1)
print(cat(4), cat(4) * factorial(4))
  1. A. 14 336
  2. B. 14 24
  3. C. 16 384
  4. D. 42 1008

Correct answer: A. 14 336

Unlabelled binary tree shapes = Catalan(4) = 14. Each shape can hold the 4 keys in 4! orders, giving 336 labelled trees – but only 14 of those are BSTs, because a BST's shape fixes where each key goes.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic