deno.land / x / masx200_leetcode_test@10.6.5 / maximum-width-of-binary-tree / 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";import { bigintMax } from "./bigint_max.ts";import { bigintMin } from "./bigint_min.ts";
export default function widthOfBinaryTree(root: TreeNode | null): number { if (!root) return 0; let current: Map<bigint, TreeNode> = new Map([[BigInt(0), root]]);
let max_width = 1;
while (true) { const latest: Map<bigint, TreeNode> = new Map(); for (const [key, value] of current) { const left = value?.left; const right = value?.right; if (left) { latest.set(BigInt(key * 2n), left); } if (right) { latest.set(BigInt(key * 2n + 1n), right); } }
if (latest.size) { current = latest;
max_width = Math.max( max_width, Number( 1n + bigintMax(...latest.keys()) - bigintMin(...latest.keys()), ), ); } else break; }
return max_width;}
masx200_leetcode_test
leetcode题解和一些测试工具
DocumentationDocumentation
GitHub Repositorymasx200/leetcode-test
GitHub Stars
4

Version Info

Tagged at
4 months ago