Data Structures & AlgorithmsVery hardProduct-company placement
A tree on vertices 1–8 has edges 2–1, 3–2, 4–1, 5–2, 6–5, 7–2, 8–1. What is the sum of the distances (in edges) over all unordered pairs of vertices?
- A. 124
- B. 62
- C. 55
- D. 69
Correct answer: B. 62
Each edge splitting the tree into parts of sizes s and n − s lies on s·(n − s) of the paths; summing over the 7 edges gives 62. Counting ordered pairs would double it.
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
- How many structurally different binary trees with 7 nodes have height EXACTLY 4 (height = edges on the longest root-to-leaf path)?
- Keys 54, 17, 39, 66, 11, 36, 83, 51, 94, 10, 76 are inserted in that order into an empty BST. Then 39 is deleted and after that 83 is…
- Prim's algorithm starts at E on the weighted undirected graph A–C (13), A–D (1), A–G (11), B–D (16), B–G (6), C–D (14), C–E (18), C–F…
- Keys 53, 59, 94, 71, 34, 76, 82, 15, 62, 24 are inserted in order into an empty AVL tree. Counting a double rotation as two single…
- With 2 identical eggs and at most 6 drops, what is the largest number of floors for which you can always determine the critical floor (the…
- How many subsets of {1, 3, 4, 5, 8, 10, 13} have sum exactly 20?