Episode 7 — DSA with JavaScript / 7.17 — Binary Search Tree
7.17.a — Binary Search Tree Operations & Algorithms
What is a BST?
A Binary Search Tree maintains the property: for every node, all values in the left subtree are less, and all values in the right subtree are greater.
8
/ \
3 10
/ \ \
1 6 14
/ \ /
4 7 13
Inorder: [1, 3, 4, 6, 7, 8, 10, 13, 14] ← sorted!
BST Search
function search(root, target) {
if (!root) return null;
if (target === root.val) return root;
if (target < root.val) return search(root.left, target);
return search(root.right, target);
}
TreeNode* search(TreeNode* root, int target) {
if (!root || root->val == target) return root;
return target < root->val ? search(root->left, target) : search(root->right, target);
}
Time: O(h) where h = height. O(log n) balanced, O(n) skewed.
BST Insert
Insert 5 into:
8
/ \
3 10
/ \
1 6
Compare: 5 < 8 → go left
5 > 3 → go right
5 < 6 → go left → insert here
8
/ \
3 10
/ \
1 6
/
5
function insert(root, val) {
if (!root) return new TreeNode(val);
if (val < root.val) root.left = insert(root.left, val);
else if (val > root.val) root.right = insert(root.right, val);
return root;
}
TreeNode* insert(TreeNode* root, int val) {
if (!root) return new TreeNode(val);
if (val < root->val) root->left = insert(root->left, val);
else if (val > root->val) root->right = insert(root->right, val);
return root;
}
BST Delete
Three cases:
- Leaf node: Simply remove
- One child: Replace with child
- Two children: Replace with inorder successor (smallest in right subtree)
function deleteNode(root, key) {
if (!root) return null;
if (key < root.val) { root.left = deleteNode(root.left, key); }
else if (key > root.val) { root.right = deleteNode(root.right, key); }
else {
if (!root.left) return root.right;
if (!root.right) return root.left;
let successor = root.right;
while (successor.left) successor = successor.left;
root.val = successor.val;
root.right = deleteNode(root.right, successor.val);
}
return root;
}
Validate BST
function isValidBST(root, min = -Infinity, max = Infinity) {
if (!root) return true;
if (root.val <= min || root.val >= max) return false;
return isValidBST(root.left, min, root.val) &&
isValidBST(root.right, root.val, max);
}
Kth Smallest
function kthSmallest(root, k) {
let count = 0, result = null;
function inorder(node) {
if (!node || result !== null) return;
inorder(node.left);
count++;
if (count === k) { result = node.val; return; }
inorder(node.right);
}
inorder(root);
return result;
}
Convert sorted array to BST
function sortedArrayToBST(nums) {
function build(lo, hi) {
if (lo > hi) return null;
const mid = Math.floor((lo + hi) / 2);
const node = new TreeNode(nums[mid]);
node.left = build(lo, mid - 1);
node.right = build(mid + 1, hi);
return node;
}
return build(0, nums.length - 1);
}
BST operations complexity
| Operation | Balanced | Skewed |
|---|---|---|
| Search | O(log n) | O(n) |
| Insert | O(log n) | O(n) |
| Delete | O(log n) | O(n) |
| Min/Max | O(log n) | O(n) |
Advantages of BST
- Sorted order traversal in O(n)
- Efficient search/insert/delete: O(log n)
- Dynamic — no fixed size
- Floor/ceiling operations
- Range queries
Scenario bank (BST)
7.17a-001 — Find inorder successor (v1)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-002 — Find inorder predecessor (v2)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-003 — Convert BST to greater tree (v3)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-004 — Recover BST (two swapped) (v4)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-005 — Trim BST to range (v5)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-006 — Find closest value (v6)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-007 — BST iterator (next smallest) (v7)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-008 — Count nodes in range (v8)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-009 — Merge two BSTs (v9)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-010 — Convert BST to doubly linked list (v10)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-011 — Largest BST subtree (v11)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-012 — Unique BSTs (Catalan) (v12)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-013 — Balance a BST (v13)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-014 — Serialize BST (v14)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-015 — Two Sum in BST (v15)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-016 — Floor and ceiling in BST (v16)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-017 — Delete range from BST (v17)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-018 — Check if preorder is valid BST (v18)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-019 — Convert sorted list to BST (v19)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-020 — Find median in BST (v20)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-021 — Find inorder successor (v21)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-022 — Find inorder predecessor (v22)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-023 — Convert BST to greater tree (v23)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-024 — Recover BST (two swapped) (v24)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-025 — Trim BST to range (v25)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-026 — Find closest value (v26)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-027 — BST iterator (next smallest) (v27)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-028 — Count nodes in range (v28)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-029 — Merge two BSTs (v29)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-030 — Convert BST to doubly linked list (v30)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-031 — Largest BST subtree (v31)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-032 — Unique BSTs (Catalan) (v32)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-033 — Balance a BST (v33)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-034 — Serialize BST (v34)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-035 — Two Sum in BST (v35)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-036 — Floor and ceiling in BST (v36)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-037 — Delete range from BST (v37)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-038 — Check if preorder is valid BST (v38)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-039 — Convert sorted list to BST (v39)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-040 — Find median in BST (v40)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-041 — Find inorder successor (v41)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-042 — Find inorder predecessor (v42)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-043 — Convert BST to greater tree (v43)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-044 — Recover BST (two swapped) (v44)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-045 — Trim BST to range (v45)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-046 — Find closest value (v46)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-047 — BST iterator (next smallest) (v47)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-048 — Count nodes in range (v48)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-049 — Merge two BSTs (v49)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-050 — Convert BST to doubly linked list (v50)
- Level: Beginner
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-051 — Largest BST subtree (v51)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-052 — Unique BSTs (Catalan) (v52)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-053 — Balance a BST (v53)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-054 — Serialize BST (v54)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-055 — Two Sum in BST (v55)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-056 — Floor and ceiling in BST (v56)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-057 — Delete range from BST (v57)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-058 — Check if preorder is valid BST (v58)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-059 — Convert sorted list to BST (v59)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-060 — Find median in BST (v60)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-061 — Find inorder successor (v61)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-062 — Find inorder predecessor (v62)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-063 — Convert BST to greater tree (v63)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-064 — Recover BST (two swapped) (v64)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-065 — Trim BST to range (v65)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-066 — Find closest value (v66)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-067 — BST iterator (next smallest) (v67)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-068 — Count nodes in range (v68)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-069 — Merge two BSTs (v69)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-070 — Convert BST to doubly linked list (v70)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-071 — Largest BST subtree (v71)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-072 — Unique BSTs (Catalan) (v72)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-073 — Balance a BST (v73)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-074 — Serialize BST (v74)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-075 — Two Sum in BST (v75)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-076 — Floor and ceiling in BST (v76)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-077 — Delete range from BST (v77)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-078 — Check if preorder is valid BST (v78)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-079 — Convert sorted list to BST (v79)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-080 — Find median in BST (v80)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-081 — Find inorder successor (v81)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-082 — Find inorder predecessor (v82)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-083 — Convert BST to greater tree (v83)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-084 — Recover BST (two swapped) (v84)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-085 — Trim BST to range (v85)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-086 — Find closest value (v86)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-087 — BST iterator (next smallest) (v87)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-088 — Count nodes in range (v88)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-089 — Merge two BSTs (v89)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-090 — Convert BST to doubly linked list (v90)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-091 — Largest BST subtree (v91)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-092 — Unique BSTs (Catalan) (v92)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-093 — Balance a BST (v93)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-094 — Serialize BST (v94)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-095 — Two Sum in BST (v95)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-096 — Floor and ceiling in BST (v96)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-097 — Delete range from BST (v97)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-098 — Check if preorder is valid BST (v98)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-099 — Convert sorted list to BST (v99)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-100 — Find median in BST (v100)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-101 — Find inorder successor (v101)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-102 — Find inorder predecessor (v102)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-103 — Convert BST to greater tree (v103)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-104 — Recover BST (two swapped) (v104)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-105 — Trim BST to range (v105)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-106 — Find closest value (v106)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-107 — BST iterator (next smallest) (v107)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-108 — Count nodes in range (v108)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-109 — Merge two BSTs (v109)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-110 — Convert BST to doubly linked list (v110)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-111 — Largest BST subtree (v111)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-112 — Unique BSTs (Catalan) (v112)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-113 — Balance a BST (v113)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-114 — Serialize BST (v114)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-115 — Two Sum in BST (v115)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-116 — Floor and ceiling in BST (v116)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-117 — Delete range from BST (v117)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-118 — Check if preorder is valid BST (v118)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-119 — Convert sorted list to BST (v119)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-120 — Find median in BST (v120)
- Level: Intermediate
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-121 — Find inorder successor (v121)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-122 — Find inorder predecessor (v122)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-123 — Convert BST to greater tree (v123)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-124 — Recover BST (two swapped) (v124)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-125 — Trim BST to range (v125)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-126 — Find closest value (v126)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-127 — BST iterator (next smallest) (v127)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-128 — Count nodes in range (v128)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-129 — Merge two BSTs (v129)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-130 — Convert BST to doubly linked list (v130)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-131 — Largest BST subtree (v131)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-132 — Unique BSTs (Catalan) (v132)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-133 — Balance a BST (v133)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-134 — Serialize BST (v134)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-135 — Two Sum in BST (v135)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-136 — Floor and ceiling in BST (v136)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-137 — Delete range from BST (v137)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-138 — Check if preorder is valid BST (v138)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-139 — Convert sorted list to BST (v139)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-140 — Find median in BST (v140)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-141 — Find inorder successor (v141)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-142 — Find inorder predecessor (v142)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-143 — Convert BST to greater tree (v143)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-144 — Recover BST (two swapped) (v144)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-145 — Trim BST to range (v145)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-146 — Find closest value (v146)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-147 — BST iterator (next smallest) (v147)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-148 — Count nodes in range (v148)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-149 — Merge two BSTs (v149)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-150 — Convert BST to doubly linked list (v150)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-151 — Largest BST subtree (v151)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-152 — Unique BSTs (Catalan) (v152)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-153 — Balance a BST (v153)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-154 — Serialize BST (v154)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-155 — Two Sum in BST (v155)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-156 — Floor and ceiling in BST (v156)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-157 — Delete range from BST (v157)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-158 — Check if preorder is valid BST (v158)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-159 — Convert sorted list to BST (v159)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-160 — Find median in BST (v160)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-161 — Find inorder successor (v161)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-162 — Find inorder predecessor (v162)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-163 — Convert BST to greater tree (v163)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-164 — Recover BST (two swapped) (v164)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-165 — Trim BST to range (v165)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-166 — Find closest value (v166)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-167 — BST iterator (next smallest) (v167)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-168 — Count nodes in range (v168)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-169 — Merge two BSTs (v169)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.
7.17a-170 — Convert BST to doubly linked list (v170)
- Level: Advanced
- Approach: Apply technique.
- Time/Space: Analyze.
- Edge cases: empty, single, boundary.
- JS & C++: Both.