Binary Search Tree (BST): Practice Problems and Interview Questions
A Binary Search Tree (BST) is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child, and the topmost node in the tree is called the root. It additionally satisfies the binary search property, which states that the key in each node must be greater than or equal to any key stored in the left subtree, and less than or equal to any key stored in the right subtree.
Binary search trees allow fast lookup, addition, and removal of items. They keep their keys in sorted order so that lookup and other operations can use the principle of binary search: when looking for a key in a tree (or a place to insert a new key), they traverse the tree from root to leaf, making comparisons to keys stored in the nodes of the tree and deciding, based on the comparison, to continue searching in the left or right subtrees. On average, this means that each comparison allows the operations to skip about half of the tree so that each lookup, insertion, or deletion takes time proportional to the logarithm of the number of items stored in the tree. This is much better than the linear time required to find items by key in an (unsorted) array but slower than the corresponding operations on hash tables.
In this post, we have listed out commonly asked interview questions on Binary Search Tree:
- Insertion in a BST
- Search a given key in BST
- Deletion from BST (Binary Search Tree)
- Construct a balanced BST from the given keys
- Determine whether a given binary tree is a BST or not
- Check if the given keys represent the same BSTs or not without building BST
- Find inorder predecessor for the given key in a BST
- Find the Lowest Common Ancestor (LCA) of two nodes in a BST
- Find k’th smallest and k’th largest element in a BST
- Find floor and ceil in a Binary Search Tree
- Convert a binary tree to BST by maintaining its original structure
- Remove nodes from a BST that have keys outside a valid range
- Find a pair with the given sum in a BST
- Find k’th smallest node in a Binary Search Tree (BST)
- Find inorder successor for the given key in a BST
- Replace every array element with the least greater element on its right
- Fix a binary tree that is only one swap away from becoming a BST
- Update every key in a BST to contain the sum of all greater keys
- Check if a given sequence represents the preorder traversal of a BST
- Build a Binary Search Tree from a postorder sequence
- Build a Binary Search Tree from a preorder sequence
- Count subtrees in a BST whose nodes lie within a given range
- Find the size of the largest BST in a binary tree
- Print complete Binary Search Tree (BST) in increasing order
- Print binary tree structure with its contents in C++
- Find optimal cost to construct a binary search tree
- Treap Data Structure
- Implementation of Treap Data Structure (Insert, Search, and Delete)
- Merge two BSTs into a doubly-linked list in sorted order
- Construct a height-balanced BST from an unbalanced BST
- Construct a height-balanced BST from a sorted doubly linked list
- Find a triplet with the given sum in a BST
- Convert a Binary Search Tree into a Min Heap
Thank for reading.