deno.land / x / masx200_leetcode_test@10.6.5 / smallest-string-starting-from-leaf / 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
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
export default function smallestFromLeaf(root: TreeNode | null): string { if (!root) return "";
const strs: string[] = []; let cur = [{ node: root, str: intToChar(root.val) }]; while (cur.length) { const tem: typeof cur = [];
for (const a of cur) { if (!a.node.left && !a.node.right) { strs.push(a.str); } if (a.node.left) { tem.push({ node: a.node.left, str: intToChar(a.node.left.val) + a.str, }); } if (a.node.right) { tem.push({ node: a.node.right, str: intToChar(a.node.right.val) + a.str, }); } } if (tem.length) { cur = tem; } else { break; } }
return strs.reduce((a, v) => (a.localeCompare(v) < 0 ? a : v));}function intToChar(i: number) { return String.fromCharCode(i + "a".charCodeAt(0));}
masx200_leetcode_test

Version Info

Tagged at
a year ago