-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path655_Print Binary Tree.js
48 lines (44 loc) · 1.25 KB
/
655_Print Binary Tree.js
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
// https://leetcode.com/problems/print-binary-tree/description/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
}
/**
* @param {TreeNode} root
* @return {string[][]}
*/
var printTree = function (root) {
let getDepth = function (node) {
if (!node) return 0;
return Math.max(getDepth(node.left), getDepth(node.right)) + 1;
}
let fillNode = function (res, node, dep, pos, totalHeight) {
if (!node) return;
res[dep - 1][pos] = node.val.toString();
fillNode(res, node.left, dep + 1, pos - Math.pow(2, (totalHeight - dep - 1)), totalHeight);
fillNode(res, node.right, dep + 1, pos + Math.pow(2, (totalHeight - dep - 1)), totalHeight);
}
let depth = getDepth(root);
let width = Math.pow(2, depth) - 1;
let result = [];
for (let i = 0; i < depth; i++) {
result[i] = [];
for (let j = 0; j < width; j++) {
result[i][j] = '';
}
}
fillNode(result, root, 1, Math.floor(width / 2), depth);
return result;
};
var root = new TreeNode(1);
root.left = new TreeNode(2);
root.left.right = new TreeNode(4);
root.right = new TreeNode(3);
console.log(printTree(root));