Leftist Heap - Problem
A leftist heap is a priority queue implemented as a binary tree where the heap property is maintained and the tree is leftist - meaning for every node, the rank of the left child is greater than or equal to the rank of the right child.
The rank of a node is defined as the length of the shortest path from that node to any external node (null). An external node has rank 0.
Implement a LeftistHeap class that supports:
insert(value)- Insert a value into the heapdeleteMin()- Remove and return the minimum valuemerge(other)- Merge with another leftist heapisEmpty()- Check if heap is emptygetMin()- Get minimum value without removing it
All operations should run in O(log n) time complexity using rank-based merging.
Input & Output
Example 1 — Basic Operations
$
Input:
operations = [["insert", 3], ["insert", 7], ["getMin"], ["deleteMin"], ["isEmpty"]]
›
Output:
[3, 3, false]
💡 Note:
Insert 3 and 7, getMin returns 3, deleteMin removes and returns 3, isEmpty returns false since 7 remains
Example 2 — Multiple Inserts and Deletes
$
Input:
operations = [["insert", 5], ["insert", 2], ["insert", 8], ["deleteMin"], ["deleteMin"], ["getMin"]]
›
Output:
[2, 5, 8]
💡 Note:
Insert 5, 2, 8. DeleteMin twice returns 2 then 5. GetMin shows remaining minimum is 8
Example 3 — Empty Heap Operations
$
Input:
operations = [["isEmpty"], ["insert", 10], ["deleteMin"], ["isEmpty"]]
›
Output:
[true, 10, true]
💡 Note:
Initially empty (true), insert 10, deleteMin returns 10, now empty again (true)
Constraints
- 1 ≤ operations.length ≤ 1000
- -104 ≤ value ≤ 104
- All operations are valid (no deleteMin on empty heap)
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code