Data Structures & AlgorithmsVery hardGATE CS
How many structurally distinct binary trees with 8 nodes have at most 4 levels (a single root alone is 1 level)?
- A. 1430
- B. 94
- C. 950
- D. 470
Correct answer: B. 94
Let B(n,h) count trees with n nodes and at most h levels: B(n,h)=sum of B(i,h-1)*B(n-1-i,h-1). Then B(8,4)=94; with no depth limit the count is the Catalan number C8=1430.
More Coding Interview Problems questions
- How many binary strings of length 12 contain neither 101 nor 010 as a substring?
- What are the last three digits of 7^2025?
- In a jump game, a[i] is the maximum jump length from index i (any length from 1 to a[i] is allowed). For a = [3, 1, 4, 1, 5, 9, 2, 6, 5…
- The sequence a1, ..., a14 is generated by a1 = 1 and a(i+1) = (5a(i) + 3) mod 17. How many pairs (i, j) with i < j satisfy a(i) > a(j)?
- In how many ways can 30 be written as a sum of distinct positive integers, each at most 10, if the order of the summands does not matter?
- An undirected weighted graph on vertices 0-5 has edges (u, v, w): (0,1,2), (0,2,2), (1,3,3), (2,3,3), (1,4,5), (2,4,5), (3,5,1), (4,5,1)…