deno.land / x / masx200_leetcode_test@10.6.5 / print-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
41
42
43
44
45
46
47
48
49
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";import maxDepth from "../maximum-depth-of-binary-tree/index.ts";
export interface BinaryTreePosition { node: TreeNode; row: number; col: number;}function printTree(root: TreeNode | null): string[][] { if (!root) { return []; }
const depth = maxDepth(root); const height = depth - 1; const m = depth; const n = 2 ** m - 1; const result: string[][] = Array(depth) .fill(0) .map(() => Array(n).fill(""));
let cur: Array<BinaryTreePosition> = [ { node: root, row: 0, col: Math.floor((n - 1) / 2) }, ]; while (cur.length) { const next: Array<BinaryTreePosition> = []; for (const { node, row, col } of cur) { if (node) { result[row][col] = node.val.toString(); node.left && next.push({ node: node.left, row: row + 1, col: col - 2 ** (height - 1 - row), }); node.right && next.push({ node: node.right, row: row + 1, col: col + 2 ** (height - 1 - row), }); } } cur = next; } return result;}export default printTree;
masx200_leetcode_test

Version Info

Tagged at
a year ago