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