/
index.ts
52 lines (50 loc) · 1.55 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
export function serialize(root: TreeNode | null): string {
if (!root) {
return "null";
}
return JSON.stringify(TreeNodeToArray(root)).replaceAll("null", "");
}
type TreeNodeArray =
| null
| [number, TreeNodeArray, TreeNodeArray]
| number
| [number, TreeNodeArray];
function TreeNodeToArray(root: TreeNode | null): TreeNodeArray {
if (!root) return null;
if (!root.left && !root.right) {
return root.val;
}
if (root.left && !root.right) {
return [root.val, TreeNodeToArray(root.left)];
}
return [root.val, TreeNodeToArray(root.left), TreeNodeToArray(root.right)];
}
function ArrayToTreeNode(array: TreeNodeArray): TreeNode | null {
// console.log('array',array)
if (array === null) return null;
if (Array.isArray(array) && array.length === 3) {
return new TreeNode(
array[0],
ArrayToTreeNode(array[1]),
ArrayToTreeNode(array[2]),
);
}
if (Array.isArray(array) && array.length === 2) {
return new TreeNode(array[0], ArrayToTreeNode(array[1]));
}
if (typeof array === "number") {
return new TreeNode(array);
}
throw new Error("not array length 3 or 2:" + array);
}
export function deserialize(data: string): TreeNode | null {
if (data === "null") {
return null;
}
const array = JSON.parse(
data.replaceAll(",,", ",null,"),
// .replaceAll(",]", ",null]"),
);
return ArrayToTreeNode(array);
}