Data Structures & AlgorithmsHardGATE CS · Advanced
A rooted tree has the vertices A–M with root A, given by parents: A is the parent of B, B is the parent of C, B is the parent of D, B is the parent of E, C is the parent of F, F is the parent of G, E is the parent of H, E is the parent of I, I is the parent of J, I is the parent of K, I is the parent of L, I is the parent of M. How many unordered pairs of distinct vertices {u, v} have vertex B as their lowest common ancestor (a vertex may be an ancestor of the other)?
- A. 45
- B. 84
- C. 42
- D. 31
Correct answer: C. 42
Let s be the size of the subtree of B and c_i the child subtree sizes. Pairs with LCA B are the pairs that are not both inside one child subtree: C(s,2) - sum C(c_i,2) = 42. Dropping the pairs that include B itself gives 31.
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
- A weighted undirected graph on A–H has the edges (weights): A–D (16), A–E (22), A–F (26), A–G (5), A–H (4), B–C (9), B–H (23), C–D (13)…
- Keys 45, 25, 65, 15, 35, 55, 75, 30 are inserted in this order into an empty binary search tree. What is its postorder traversal?
- What is the maximum number of nodes in a binary tree of height 5, where the height is the number of edges on the longest root-to-leaf path?
- A complete binary tree stored level by level has exactly 40 nodes. How many of them are leaves?
- Keys 14, 8, 11, 5, 9, 3, 12 are inserted one at a time into an initially empty binary min-heap kept in an array. What does the array hold…
- An undirected graph has edges A-B, A-C, B-D, B-E, C-E, C-F, D-G, E-G, F-G. Neighbours are always visited in alphabetical order. What is…