ITPEC FE Subject B April 2026 Question 9
From the answer group below, select the correct combination of answers to be inserted into A and B in the program.
The function isPerfectBinaryTree checks whether the binary tree with a given root node is perfect. In a perfect binary tree, all the internal nodes have exactly two children, and all leaf nodes are at the same level. The function returns true if the tree is perfect, or false otherwise.
The class Node models a node of a binary tree as described in Table 1. A Node-type variable holds a reference to an instance of the class Node.
Table 1 Class Node
Table 2 describes the functions used in the program.
Table 2 Functions
Answer Group
Explanation
A: size + 1 = pow(2, height), B: getSize(root.left) + getSize(root.right) + 1 — a perfect binary tree of height h holds exactly 2h − 1 nodes.
- •B: the size of a subtree is the sizes of its two child subtrees plus the node itself, hence the
+ 1 - •A:
getHeightcounts levels (an empty tree is 0, one node is 1). A perfect tree has 1 + 2 + 4 + … + 2h−1 = 2h − 1 nodes, and no other tree of that height has as many, so adding 1 tosizemust givepow(2, height)
Check the smallest trees:
- •one node: height 1, size 1, and 1 + 1 = 21, so
true - •root with two children: height 2, size 3, and 3 + 1 = 22, so
true - •root with one child: height 2, size 2, and 2 + 1 ≠ 22, so
false
Answer (b)
Why not others:
- •(a), (d) and (g) — omit the
+ 1in B, so every call adds only zeros andgetSizealways returns 0 - •(c), (f) and (i) — subtract 1 in B, so a leaf counts as −1 and every size comes out too small
- •(d), (e) and (f) — compare
sizewith 2h−1, the number of leaves of a perfect tree rather than all its nodes; a root with two children has size 3, not 21 = 2 - •(g), (h) and (i) — compare
sizewith 2h, one more than a perfect tree's size, so they never returntruefor a non-empty tree
Key rule: Check a size or height formula on the smallest trees (one node, then two levels); they show at once whether a 1 must be added or subtracted, and on which side.
Binary Search Tree · AI-written, may contain errors
This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.