Data Structures & AlgorithmsModerateGATE CS
A complete binary tree (array form, all levels full except possibly the last, which is filled from the left) has 131 nodes. How many leaves does it have, and how many nodes have exactly one child?
- A. 65 leaves, 0 with one child
- B. 4 leaves, 0 with one child
- C. 66 leaves, 1 with one child
- D. 66 leaves, 0 with one child
Correct answer: D. 66 leaves, 0 with one child
In array form (1-based), nodes 1..⌊n/2⌋ = 1..65 are internal, so leaves = 131 − 65 = 66; the last internal node has only a left child exactly when n is even. The 4 nodes on the last level are not all the leaves.
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
- Six sorted files have 10, 12, 13, 21, 23, 42 records. Merging two files of sizes a and b costs a + b. What is the minimum total cost to…
- Five matrices have dimensions 3×2 , 2×4 , 4×2 , 2×8 , 8×10. What is the minimum number of scalar multiplications needed to compute their…
- Keys 17, 35, 21, 81, 58, 30, 62, 57, 82, 93, 61 are inserted in that order into an empty BST. Then 58 is deleted and after that 35 is…
- Using parts 1, 2, 3 (each any number of times), in how many ways can 7 be written as an ORDERED sum (so 1 + 2 and 2 + 1 count separately)?
- For the string ABRACADABRA, what are the lengths of the longest palindromic SUBSTRING and the longest palindromic SUBSEQUENCE?
- Heap sort (ascending) is run on [20, 37, 49, 16, 22, 10, 31, 33]: build a max-heap bottom-up, then repeatedly swap the root with the last…