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))
- A. 14 336
- B. 14 24
- C. 16 384
- 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.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- What is printed? (number of nodes in a complete binary tree in O(log² n)) python def count(nnodes): # implicit complete tree with nodes…
- What is printed? (AVL: which rotation is needed after inserting 30, 10, 20 into an empty AVL tree?) python def balancecase(order): a, b, c…
- What is the minimum number of nodes in an AVL tree of height h (height of a single node = 0)? python def nmin(h): a, b = 1, 2 # N(0), N(1)…
- BFS from vertex 0 visits neighbours in increasing order. What is the BFS order and the DFS (recursive) order? python g = {0: [1, 2], 1…
- Run Dijkstra from A on the weighted directed graph. In what order are vertices finalised (popped), and what is dist[E]? python import…
- Prim's algorithm from vertex 0 on the undirected graph below. In what order are vertices added and what is the MST weight? python import…