|
| 1 | +// Time complexity: O(nlogn) |
| 2 | +// Space complexity: O(n) |
| 3 | + |
| 4 | +class _Queue { |
| 5 | + constructor() { |
| 6 | + this.q = []; |
| 7 | + this.front = 0; |
| 8 | + this.rear = 0; |
| 9 | + } |
| 10 | + |
| 11 | + push(value) { |
| 12 | + this.q.push(value); |
| 13 | + this.rear++; |
| 14 | + } |
| 15 | + |
| 16 | + shift() { |
| 17 | + const rv = this.q[this.front]; |
| 18 | + delete this.q[this.front++]; |
| 19 | + return rv; |
| 20 | + } |
| 21 | + |
| 22 | + isEmpty() { |
| 23 | + return this.front === this.rear; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +class MinHeap { |
| 28 | + constructor() { |
| 29 | + this.heap = [null]; |
| 30 | + } |
| 31 | + |
| 32 | + push(value) { |
| 33 | + this.heap.push(value); |
| 34 | + |
| 35 | + let current = this.heap.length - 1; |
| 36 | + let parent = Math.floor(current / 2); |
| 37 | + |
| 38 | + while (parent && this.heap[current] < this.heap[parent]) { |
| 39 | + [this.heap[current], this.heap[parent]] = [ |
| 40 | + this.heap[parent], |
| 41 | + this.heap[current], |
| 42 | + ]; |
| 43 | + current = parent; |
| 44 | + parent = Math.floor(current / 2); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + pop() { |
| 49 | + if (this.heap.length === 1) { |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + if (this.heap.length === 2) { |
| 54 | + return this.heap.pop(); |
| 55 | + } |
| 56 | + |
| 57 | + const rv = this.heap[1]; |
| 58 | + this.heap[1] = this.heap.pop(); |
| 59 | + |
| 60 | + let current = 1; |
| 61 | + let left = current * 2; |
| 62 | + let right = left + 1; |
| 63 | + |
| 64 | + while ( |
| 65 | + (this.heap[left] && this.heap[current] > this.heap[left]) || |
| 66 | + (this.heap[right] && this.heap[current] > this.heap[right]) |
| 67 | + ) { |
| 68 | + if (this.heap[right] && this.heap[right] < this.heap[left]) { |
| 69 | + [this.heap[right], this.heap[current]] = [ |
| 70 | + this.heap[current], |
| 71 | + this.heap[right], |
| 72 | + ]; |
| 73 | + current = right; |
| 74 | + } else { |
| 75 | + [this.heap[left], this.heap[current]] = [ |
| 76 | + this.heap[current], |
| 77 | + this.heap[left], |
| 78 | + ]; |
| 79 | + current = left; |
| 80 | + } |
| 81 | + |
| 82 | + left = current * 2; |
| 83 | + right = left + 1; |
| 84 | + } |
| 85 | + |
| 86 | + return rv; |
| 87 | + } |
| 88 | +} |
| 89 | +/** |
| 90 | + * Definition for a binary tree node. |
| 91 | + * function TreeNode(val, left, right) { |
| 92 | + * this.val = (val===undefined ? 0 : val) |
| 93 | + * this.left = (left===undefined ? null : left) |
| 94 | + * this.right = (right===undefined ? null : right) |
| 95 | + * } |
| 96 | + */ |
| 97 | +/** |
| 98 | + * @param {TreeNode} root |
| 99 | + * @param {number} k |
| 100 | + * @return {number} |
| 101 | + */ |
| 102 | +var kthSmallest = function (root, k) { |
| 103 | + const minHeap = new MinHeap(); |
| 104 | + |
| 105 | + const q = new _Queue(); |
| 106 | + q.push(root); |
| 107 | + |
| 108 | + while (!q.isEmpty()) { |
| 109 | + const current = q.shift(); |
| 110 | + |
| 111 | + minHeap.push(current.val); |
| 112 | + |
| 113 | + if (current.left) { |
| 114 | + q.push(current.left); |
| 115 | + } |
| 116 | + |
| 117 | + if (current.right) { |
| 118 | + q.push(current.right); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + let answer = null; |
| 123 | + for (let i = 0; i < k; i++) { |
| 124 | + answer = minHeap.pop(); |
| 125 | + } |
| 126 | + |
| 127 | + return answer; |
| 128 | +}; |
0 commit comments