Binary Search Tree (BST): Practice Problems and Interview Questions

Vivek Srivastava
Techie Delight
3 min readDec 17, 2018

--

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:

  1. Insertion in a BST
  2. Search a given key in BST
  3. Deletion from BST (Binary Search Tree)
  4. Construct a balanced BST from the given keys
  5. Determine whether a given binary tree is a BST or not
  6. Check if the given keys represent the same BSTs or not without building BST
  7. Find inorder predecessor for the given key in a BST
  8. Find the Lowest Common Ancestor (LCA) of two nodes in a BST
  9. Find k’th smallest and k’th largest element in a BST
  10. Find floor and ceil in a Binary Search Tree
  11. Convert a binary tree to BST by maintaining its original structure
  12. Remove nodes from a BST that have keys outside a valid range
  13. Find a pair with the given sum in a BST
  14. Find k’th smallest node in a Binary Search Tree (BST)
  15. Find inorder successor for the given key in a BST
  16. Replace every array element with the least greater element on its right
  17. Fix a binary tree that is only one swap away from becoming a BST
  18. Update every key in a BST to contain the sum of all greater keys
  19. Check if a given sequence represents the preorder traversal of a BST
  20. Build a Binary Search Tree from a postorder sequence
  21. Build a Binary Search Tree from a preorder sequence
  22. Count subtrees in a BST whose nodes lie within a given range
  23. Find the size of the largest BST in a binary tree
  24. Print complete Binary Search Tree (BST) in increasing order
  25. Print binary tree structure with its contents in C++
  26. Find optimal cost to construct a binary search tree
  27. Treap Data Structure
  28. Implementation of Treap Data Structure (Insert, Search, and Delete)
  29. Merge two BSTs into a doubly-linked list in sorted order
  30. Construct a height-balanced BST from an unbalanced BST
  31. Construct a height-balanced BST from a sorted doubly linked list
  32. Find a triplet with the given sum in a BST
  33. Convert a Binary Search Tree into a Min Heap

Thank for reading.

--

--