Convert BST to Min Heap
Pattern: Heap implementation
Read the BST's values in order: they come out sorted. Then walk the same tree in preorder (node, left, right) and write the sorted values back one by one. Each node gets a value smaller than everything below it, and every left subtree holds smaller values than the right one.
The problem
Given a complete binary search tree, change its values (not its shape) so that it becomes a min heap in which every
value in a node's left subtree is smaller than every value in its right subtree. The BST [4, 2, 6, 1, 3, 5, 7] becomes
[1, 2, 5, 3, 4, 6, 7]. The function returns nothing; the tests print the tree in level order.
Think first
- How do you get a BST's values in sorted order?
- Which traversal visits a node before everything below it, and the left side before the right?
- Why does that order give both rules at once?
Hints
Brute force, and why it fails
Copy the values into an array, build a heap and write it back level by level: a heap, but the left-smaller-than-right rule can fail.
The key insight
Sorted values written in preorder satisfy the heap rule and the left-right rule together, and the shape never changes.
Dry run
| sorted | 1 2 3 4 5 6 7 |
|---|
BST [4, 2, 6, 1, 3, 5, 7]. Inorder gives 1, 2, 3, 4, 5, 6, 7.
The plan
values = inorder(root) # sorted
i = 0
preorder(node): node.val = values[i]; i += 1; preorder(left); preorder(right)
Complexity
- Time
O(n)- Space
O(n)
Two traversals and an array of the values.
Edge cases
- One node.
- Two nodes: the root takes the smaller value.
- The shape stays the same; only values move.
I've built my logic, show the code
Java
import java.util.*;
class Solution {
public void convertToMinHeap(TreeNode root) {
List<Integer> sorted = new ArrayList<>();
inorder(root, sorted); // a BST's inorder is sorted
int[] next = {0};
preorder(root, sorted, next); // write them back root, left, right
}
private void inorder(TreeNode node, List<Integer> out) {
if (node == null) return;
inorder(node.left, out);
out.add(node.val);
inorder(node.right, out);
}
private void preorder(TreeNode node, List<Integer> sorted, int[] next) {
if (node == null) return;
node.val = sorted.get(next[0]++); // a parent gets a smaller value than everything below it
preorder(node.left, sorted, next);
preorder(node.right, sorted, next);
}
}C++
class Solution {
public:
void convertToMinHeap(TreeNode* root) {
vector<int> sorted;
inorder(root, sorted); // a BST's inorder is sorted
size_t next = 0;
preorder(root, sorted, next); // write them back root, left, right
}
void inorder(TreeNode* node, vector<int>& out) {
if (node == NULL) return;
inorder(node->left, out);
out.push_back(node->val);
inorder(node->right, out);
}
void preorder(TreeNode* node, const vector<int>& sorted, size_t& next) {
if (node == NULL) return;
node->val = sorted[next++]; // a parent gets a smaller value than everything below it
preorder(node->left, sorted, next);
preorder(node->right, sorted, next);
}
};Python
class Solution:
def convertToMinHeap(self, root) -> None:
values = []
def inorder(node): # a BST's inorder is sorted
if node:
inorder(node.left)
values.append(node.val)
inorder(node.right)
it = iter([])
def preorder(node): # write them back root, left, right
if node:
node.val = next(it) # a parent gets a smaller value than everything below it
preorder(node.left)
preorder(node.right)
inorder(root)
it = iter(values)
preorder(root)Common mistakes
- Writing values back in inorder, which leaves the tree a BST.
- Writing in level order, which breaks the left-right rule.