deno.land / x / masx200_leetcode_test@10.6.5 / utils / PrefixTreeToArray1.ts

PrefixTreeToArray1.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 { PrefixTree } from "../implement-trie-prefix-tree/PrefixTree.ts";
//小心调用栈溢出// export function PrefixTreeToArray(root: PrefixTree): Array<string> {// const r: Array<string> = [];// traverse(root, (s) => r.push(s));// return r;// }// function traverse(root: PrefixTree, output: (s: string) => void) {// root.children.forEach(function (child, key) {// // console.log(key);// if (child.isEnd) {// output(key);// }// return traverse(child, (s: string) => output(key + s));// });// }//循环export function PrefixTreeToArray1(root: PrefixTree): Array<string> { if (root.children.size === 0) { return []; } const res: Array<string> = []; let prefix_and_entries: { prefix: string; key: string; child: PrefixTree; }[] = [...root.children].map((e) => ({ prefix: "", key: e[0], child: e[1], })); while (prefix_and_entries.length > 0) { const temp: typeof prefix_and_entries = []; for (const entry of prefix_and_entries) { const { prefix, key, child } = entry; if (child.isEnd) { res.push(prefix + key); } for (const e of child.children) { temp.push({ prefix: prefix + key, key: e[0], child: e[1], }); } } prefix_and_entries = temp; } return res;}
masx200_leetcode_test

Version Info

Tagged at
a year ago