-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path655 Print Binary Tree.js
More file actions
45 lines (41 loc) · 962 Bytes
/
655 Print Binary Tree.js
File metadata and controls
45 lines (41 loc) · 962 Bytes
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
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
const helper = (result, node, level, left, right) => {
if (node === null) {
return
}
let index = (left + right) / 2
result[level][index] = `${node.val}`
helper(result, node.left, level + 1, left, index - 1)
helper(result, node.right, level + 1, index + 1, right)
}
const getHeight = (node) => {
if (node === null) {
return 0
}
return Math.max(1 + getHeight(node.left), 1 + getHeight(node.right))
}
/**
* @param {TreeNode} root
* @return {string[][]}
*/
const printTree = (root) => {
let height = getHeight(root)
let size = 2 ** height - 1
let result = []
for (let i = 0; i < height; i++) {
const list = []
for (let j = 0; j < size; j++) {
list.push('')
}
result.push(list)
}
helper(result, root, 0, 0, size - 1)
return result
}
console.log(printTree())