-
Notifications
You must be signed in to change notification settings - Fork 2
/
diameter_of_binary_tree.js
60 lines (57 loc) · 1.29 KB
/
diameter_of_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
49
50
51
52
53
54
55
56
57
58
59
60
class Node {
constructor(value){
this.value = value;
this.left = null;
this.right = null;
}
}
class BinaryTree {
constructor(){
this.root = null;
this.length = 0;
}
add(value){
const node = new Node(value);
if(this.length == 0){
this.root = node;
} else {
let queue = [this.root];
while(queue.length){
if(!queue[0].left){
queue[0].left = node;
return this;
}
if(!queue[0].right){
queue[0].right = node;
return this;
}
queue.push(queue[0].left);
queue.push(queue[0].right);
queue.shift();
}
}
this.length++;
return this;
}
}
var diameterOfBinaryTree = function(root) {
let maxChildren = 0;
function helper(root) {
const leftChildren = root.left ? helper(root.left) : 0;
const rightChildren = root.right ? helper(root.right) : 0;
const children = leftChildren + rightChildren;
if(children > maxChildren) maxChildren = children;
return 1 + Math.max(leftChildren, rightChildren);
}
helper(root);
return maxChildren;
};
const tree = new BinaryTree();
tree.add(1);
tree.add(2);
tree.add(3);
tree.add(4);
tree.add(5);
console.log(tree);
const diameter = diameterOfBinaryTree(tree.root);
console.log('diameter:', diameter);