Prepin
Log in

Insertion in a Binary Search Tree (BST)

Approach / Explanation

This question requests you to explain how to add a new node to a Binary Search Tree (BST), a specific kind of binary tree structure. Your description should cover the following key points: The definition and characteristics of a BST. The method for finding the appropriate location to insert the new node. The steps involved in inserting the new node once the correct location has been determined.

Suggested Answer

A Binary Search Tree (BST) is a type of tree data structure where each node can have up to two children, known as the left child and right child. In a BST, all nodes located to the left of a node x are less than or equal to x, while all nodes to the right of x are greater than x. When adding a new node to a BST, we begin at the root node. The new node is navigated down the tree, moving left if it is less than the current node and right if it is greater. If the child in the direction we want to go is null, we insert the new node in that position. For instance, to insert 15 into the following BST: 10 / \ 5 20 / \ 4 7 We start at the root (10), and since 15 is greater than 10, we proceed to the right. Now at 20, we see that 15 is less than 20, so we move to the left. As the left side of 20 is empty, we insert the new node 15 there. The updated BST will look like this: 10 / \ 5 20 / \ / 4 7 15

Alternative Answer

To add a node to a Binary Search Tree (BST), you should begin by comparing the value of the new node with the value of the current root. If the new value is smaller, move to the left child; if it's larger, proceed to the right child. Repeat this process until you find an empty location where the new node can be placed. For example, if you want to insert 25 into the following BST: 30 / \ 20 40 / \ / \ 10 24 34 50 You would start with the root (30). Since 25 is less than 30, you would move left. Next, because 25 is greater than 20, you would go right. When you reach 24, since 25 is greater than 24 and there is no right child, you would insert 25 as the right child of 24. The updated BST would look like this: 30 / \ 20 40 / \ / \ 10 24 34 50 \ 25 Remember that this process is recursive and runs in logarithmic time complexity, as the problem size is halved with each step.

Question Details

Difficulty & Category

Medium

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.