-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrint_binary_tree.js
More file actions
42 lines (42 loc) · 1.09 KB
/
Print_binary_tree.js
File metadata and controls
42 lines (42 loc) · 1.09 KB
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
this.print = function(){
function across(level, root1){
var arr = [];
for (var i=0; i<Math.pow(2, level); i++){
var current = root1;
var map = i.toString(2);
var len = map.length;
for (var j=0; j < level - len; j++){
map = '0' + map;
}
for (var j=0; j<map.length; j++){
if (map[j] == '0'){
current = current.left;
}
else {
current = current.right;
}
if (!current){
arr.push(' ');
break;
}
}
if (current){
arr.push(current.val);
}
}
return arr;
}
if (!this.root){
return;
}
console.log('*' + this.root.val);
var h = this.height();
for (var i=1; i<h; i++){
var arr = across(i, this.root);
var p = '*';
for (var j=0; j<arr.length; j++){
p += arr[j] + '*';
}
console.log(p);
}
}