Binary Search Tree
Keys of BST must be unique
Binary Search Trees are excellent data structure for storing the entries of a map. A binary search tree is a binary tree T such that each internal node v of T stores an entry (k,x) such that:
- Keys in the left subtree of k <= k.key()
- Keys in the right subtree of k >= k.key()
- only internal nodes store entries
An inorder traversal of a binary search tree should visit the keys in nondecreasing order.
Binary Tree Operations
- [Insert]
701.Insert into a Binary Search Tree (medium)
Leetcode: https://leetcode.com/problems/insert-into-a-binary-search-tree/description/
[Solution] If val < root, go left, else go right. If reach to the end of searching, append (return) the new node.
1 | class Solution { |
Leetcode: https://leetcode.com/problems/delete-node-in-a-bst/description/
Instruction: https://www.youtube.com/watch?v=gcULXE7ViZw
[Solution]
- Binary Search for the key, if key > root, go right, else go left
- if key == root, there are three situations:
a. root is a leaf node -> directly return null to delete root
b. root only has left subtree -> directly return left subtree to delete root
c. root only has right subtree -> same above
d. root has both left and right subtree: find the maximum value in left subtree (or minimum in right subtree), replace root with that value, and delete that node
1 | class Solution { |
Recursion
- Every recursion algorithm can be converted to iterative algorithm with
queue
orstack
- Construct recursion:
- Construct base case
- Construct helper method and pass status in the parameter
783. Minimum Distance Between BST Nodes
[Solution] In-order traverse the tree and compare the distance of root and previous value.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17class Solution {
Integer min = Integer.MAX_VALUE;
Integer prev = null;
public int getMinimumDifference(TreeNode root) {
dfs(root);
return min;
}
public void dfs(TreeNode root) {
if (root == null) return;
if (root.left != null) dfs(root.left);
if (prev != null) min = Math.min(min, Math.abs(root.val - prev));
prev = root.val;
if (root.right != null) dfs(root.right);
}
}
230. Kth Smallest Element in a BST
Leetcode: https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/
[DFS Solution] In-order traverse the tree and select the kth visited node
1 | class Solution { |
[Binary Search Solution] Root’s left branch all smaller than root itself, while its right branch must larger than itself. First determine where the k locates, is it in the left branch or right branch by comparing k with count of left branch nodes and right branch nodes, and then find the count+1
th node to return.
1 | public int kthSmallest(TreeNode root, int k) { |
700. Search in a Binary Search Tree (easy)
Leetcode: https://leetcode.com/problems/search-in-a-binary-search-tree/description/
1 | class Solution { |
95. Unique Binary Search Tree II
Leetcode: https://leetcode.com/problems/unique-binary-search-trees-ii/description/
Solution please see Dynamic Programming part
99. Recover Binary Search Tree
Leetcode: https://leetcode.com/problems/recover-binary-search-tree/description/
[Solution] See Discussion: https://leetcode.com/problems/recover-binary-search-tree/discuss/32535/No-Fancy-Algorithm-just-Simple-and-Powerful-In-Order-Traversal
1 | class Solution { |
109. Convert Sorted List to Binary Search Tree
Leetcode: https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/description/
[Solution] See Two Pointers -> fast and slow
426. Convert Binary Search Tree to Sorted Doubly Linked List
Leetcode: https://leetcode.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/description/
[Solution]
- Set a
prevNode
to remember the previous node - Set a
headNode
to remmeber the head of the linkedlist - in-order traverse the tree and link nodes together
1 | class Solution { |