ITPEC FE Subject B April 2025 Question 9

Source exam: ITPEC FE Subject B April 2025Topic: Binary Search Tree

ITPEC FE Subject B April 2025 — Question 9 of 20

Binary Tree Equality Check — fill in logical operators for a recursive same-tree comparison.

The function isSameTree(p, q) checks whether two binary trees are identical in both structure and values, using recursion.

Blank A — `and`:
if (p = undefined and q = undefined) → both nodes are absent, so the subtrees match at this point → return true.

Blank B — `or`:
if (p = undefined or q = undefined) → if we reach here, we know they're NOT both undefined (Blank A didn't trigger). So if either is undefined while the other isn't, the structures differ → return false.

Blank C — `and`:
return checkLeft and checkRight → the trees are identical only if both the left and right subtrees are identical.

Why not others:
- (a) or / and / and — Blank A with or would return true even when only one node is undefined, incorrectly treating mismatched structures as identical

- (b) and / or / or — Blank C with or would return true if only one subtree matches, allowing structurally different trees to pass

- (d) or / and / or — Blank A with or breaks the base case; Blank B with and only catches when both are undefined (already handled), missing the one-null case

Key rule: Base cases first — check both-null (and), then either-null (or), then compare values, then recurse on children requiring both to match (and).

AI-generated — may contain errors

The original exam layout is preserved in the image so diagrams, formulas, tables, and code remain accurate.

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.