Suppose the values are inserted in that order into an initially empty binary search tree. Let be the resulting binary search tree. The number of edges in the path from the node containing 19 to the root node of is ___________. (Answer in integer)
GATE 2025 CS2 Q.35: solution and answer
Answer and solution
ANSWER
The number of edges in the path from the node containing 19 to the root is 4.
Official answer: 4Source: Official answer key, GATE 2025 CS2 (IIT Roorkee)
Insert 10 as the root.
Insert -4: compare with 10 (-4<10) → becomes left child of 10.
Insert 15: compare with 10 (15>10) → becomes right child of 10.
Insert 30: compare with 10 (30>10, go right to 15), then with 15 (30>15) → becomes right child of 15.
Insert 20: compare with 10 (go right to 15), with 15 (go right to 30), with 30 (20<30) → becomes left child of 30.
Insert 5: compare with 10 (5<10, go left to -4), with -4 (5>-4) → becomes right child of -4.
Insert 60: compare with 10 (go right to 15), with 15 (go right to 30), with 30 (60>30) → becomes right child of 30.
Insert 19: compare with 10 (go right to 15), with 15 (go right to 30), with 30 (19<30, go left to 20), with 20 (19<20) → becomes left child of 20.
Path from 19 to the root: 19 → 20 → 30 → 15 → 10 — this path has 4 edges.
KEY IDEA
Standard BST insertion: each new key is compared against nodes starting from the root, going left if smaller and right if larger, until an empty spot is found; the depth of a node equals the number of edges on its unique root path.
WATCH OUT FOR
Counting the number of nodes on the path (5) instead of the number of edges (4), or inserting 5 relative to 10 instead of continuing the BST search down to -4's subtree (5 must be compared against -4, since 5 < 10 sends it left first).
From the GATE 2025 CS2 paper. Every question of this paper · Download the full paper · All 26 Trees & heaps questions, with notes
More Trees & heaps questions
- GATE 2024 CS1 Q.41 · MCQ · 2 marksAn array [82, 101, 90, 11, 111, 75, 33, 131, 44, 93] is heapified. Which one of the following options…
- GATE 2024 CS1 Q.43 · MCQ · 2 marksConsider a binary min-heap containing 105 distinct elements. Let k be the index (in the underlying array) of…
- GATE 2024 CS2 Q.39 · MCQ · 2 marksYou are given a set V of distinct integers. A binary search tree T is created by inserting all elements of V…
- GATE 2024 DA Q.28 · MSQ · 1 markConsider the following tree traversals on a full binary tree: (i) Preorder (ii) Inorder (iii) Postorder…
