Data Structures & AlgorithmsModerateGATE CS · ICPC regional
Problem: How many binary trees (distinct shapes, ignoring labels) can be formed with 6 nodes?
- A. 42
- B. 429
- C. 720
- D. 132
Correct answer: D. 132
Binary-tree shapes with n nodes are counted by the Catalan number C(n) = Σ C(i)·C(n − 1 − i), so C(6) = 132. The neighbouring Catalan numbers 42 and 429 come from an off-by-one in n, and 6! = 720 counts labelled orderings.
More Coding Interview Problems questions
- Problem: Count the integers from 1 to 64 whose binary representation has an even number of 1-bits.
- Problem: For the string "aaabbbbcc" with at most one replacement of any character by any other letter, what is the length of the longest…
- Problem: The array [1, 4, 3, 2] (with even length) is split into two-element pairs in any way, and the minimum of each pair is added up…
- Problem: A matrix stores the number of paths in a 3×3 grid where each cell can be entered from the left or from above, and blocked cells…
- Problem: The string "racecar" has length 7. How many of its substrings are palindromes with length at least 3? Count index ranges.
- Problem: Count the pairs (i, j) with i < j in the array [1, 5, 2, 8, 3, 5] whose absolute difference is at most 2.