-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
44 lines (36 loc) · 1.17 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
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
import { bigint_max } from "./bigint_max.ts";
import { bigint_min } 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();
current.forEach((value, key) => {
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) {
// console.log(latest)
current = latest;
max_width = Math.max(
max_width,
Number(
1n +
bigint_max(...latest.keys()) -
bigint_min(...latest.keys()),
),
);
} else break;
}
return max_width;
}