Data Structures & AlgorithmsModerateGATE CS · Product-company placement
A rooted tree has 7 nodes with 1 child, 2 nodes with 2 children, 4 nodes with 3 children, 8 nodes with 4 children, and every other node is a leaf. How many leaves does it have?
- A. 35
- B. 34
- C. 22
- D. 42
Correct answer: A. 35
Edges = nodes − 1 counted two ways: Σ i·nᵢ = (n₀ + Σ nᵢ) − 1, so n₀ = 1 + Σ (i − 1)·nᵢ = 35. Nodes with one child add nothing.
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
- In how many ways can a convex polygon with 7 vertices be divided into triangles by non-crossing diagonals?
- Coins of denominations 1, 2, 8, 10 are available in unlimited supply. What is the minimum number of coins needed to make 16, and how many…
- Heap sort (ascending) is run on [3, 13, 29, 24, 7, 25, 19, 40]: build a max-heap bottom-up, then repeatedly swap the root with the last…
- Dijkstra's algorithm runs from A on the directed graph A→C (4), A→D (15), B→A (2), B→C (13), C→D (8), C→E (7), C→F (2), D→B (12), E→B (7)…
- Floyd–Warshall runs on vertices 1–4 with directed edges 1→2 (2), 1→3 (8), 2→1 (8), 2→4 (3), 3→2 (7), 3→4 (6), 4→1 (2). After the…
- A tree on vertices 1–9 has edges 2–1, 3–2, 4–2, 5–2, 6–5, 7–4, 8–5, 9–7. What are its diameter and radius, and which vertex/vertices form…