|
| 1 | +/** |
| 2 | + * 문제 설명 |
| 3 | + * - 이진 트리를 직렬화/역직렬화 하는 문제 |
| 4 | + * |
| 5 | + * 아이디어 |
| 6 | + * 1) 트리를 순회하면서 value값만 담는다. |
| 7 | + * - DFS, BFS 둘 다 가능 |
| 8 | + * - DFS는 재귀 기반이라 코드가 간결하고, BFS 보다 메모리 사용량은 적지만, 깊은 트리에서는 스택 오버플로우가 발생할 수 있다. |
| 9 | + * - 반면에 BFS는 큐를 많이 사용해서 폭이 넓은 트리에서 비효율적일 수 있지만, |
| 10 | + */ |
| 11 | + |
| 12 | +class TreeNode { |
| 13 | + val: number; |
| 14 | + left: TreeNode | null; |
| 15 | + right: TreeNode | null; |
| 16 | + constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { |
| 17 | + this.val = val === undefined ? 0 : val; |
| 18 | + this.left = left === undefined ? null : left; |
| 19 | + this.right = right === undefined ? null : right; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +/* |
| 24 | + * Encodes a tree to a single string. |
| 25 | + */ |
| 26 | +function serialize(root: TreeNode | null): string { |
| 27 | + const result: string[] = []; |
| 28 | + |
| 29 | + const dfs = (node: TreeNode | null) => { |
| 30 | + if (node === null) { |
| 31 | + result.push("null"); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + result.push(node.val.toString()); |
| 36 | + dfs(node.left); |
| 37 | + dfs(node.right); |
| 38 | + }; |
| 39 | + |
| 40 | + dfs(root); |
| 41 | + return result.join(","); |
| 42 | +} |
| 43 | + |
| 44 | +/* |
| 45 | + * Decodes your encoded data to tree. |
| 46 | + */ |
| 47 | +function deserialize(data: string): TreeNode | null { |
| 48 | + const values = data.split(","); |
| 49 | + let i = 0; |
| 50 | + const dfs = () => { |
| 51 | + if (values[i] === "null") { |
| 52 | + i++; |
| 53 | + return null; |
| 54 | + } |
| 55 | + |
| 56 | + const node = new TreeNode(Number(values[i])); |
| 57 | + i++; |
| 58 | + node.left = dfs(); |
| 59 | + node.right = dfs(); |
| 60 | + return node; |
| 61 | + }; |
| 62 | + return dfs(); |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Your functions will be called as such: |
| 67 | + * deserialize(serialize(root)); |
| 68 | + */ |
| 69 | + |
| 70 | +/* |
| 71 | + * Encodes a tree to a single string. |
| 72 | + */ |
| 73 | +function serializeBFS(root: TreeNode | null): string { |
| 74 | + if (!root) return "null"; |
| 75 | + |
| 76 | + const queue: (TreeNode | null)[] = [root]; |
| 77 | + const result: string[] = []; |
| 78 | + |
| 79 | + while (queue.length > 0) { |
| 80 | + const node = queue.shift(); |
| 81 | + if (node) { |
| 82 | + result.push(node.val.toString()); |
| 83 | + queue.push(node.left); |
| 84 | + queue.push(node.right); |
| 85 | + } else { |
| 86 | + result.push("null"); |
| 87 | + } |
| 88 | + } |
| 89 | + return result.join(","); |
| 90 | +} |
| 91 | + |
| 92 | +/* |
| 93 | + * Decodes your encoded data to tree. |
| 94 | + */ |
| 95 | +function deserializeBFS(data: string): TreeNode | null { |
| 96 | + const values = data.split(","); |
| 97 | + |
| 98 | + if (values[0] === "null") return null; |
| 99 | + |
| 100 | + const root = new TreeNode(Number(values[0])); |
| 101 | + let i = 1; |
| 102 | + const queue: (TreeNode | null)[] = [root]; |
| 103 | + |
| 104 | + while (queue.length > 0 && i < values.length) { |
| 105 | + const current = queue.shift(); |
| 106 | + const left = values[i++]; |
| 107 | + const right = values[i++]; |
| 108 | + |
| 109 | + if (left !== "null") { |
| 110 | + current!.left = new TreeNode(Number(left)); |
| 111 | + queue.push(current!.left); |
| 112 | + } |
| 113 | + |
| 114 | + if (right !== "null") { |
| 115 | + current!.right = new TreeNode(Number(right)); |
| 116 | + queue.push(current!.right); |
| 117 | + } |
| 118 | + } |
| 119 | + return root; |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Your functions will be called as such: |
| 124 | + * deserialize(serialize(root)); |
| 125 | + */ |
0 commit comments