-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbalanced-binary-tree.js
More file actions
47 lines (40 loc) · 1.24 KB
/
Copy pathbalanced-binary-tree.js
File metadata and controls
47 lines (40 loc) · 1.24 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
43
44
45
46
47
/*
* Submission date: March 19, 2019
* Runtime: 72ms
* Faster than: 91.11%
* Memory Usage: 37.3 MB
* Less than: 78.35%
*/
// simple brute force algorithm
// checks every single node to see if its balanced
// to check if a node is balanced is to check if the maximal height
// of its two children nodes are at most 1 apart
// there are two recursive functions at play in this solution
// can be optimized to be much faster by checking only
// a nodes two subtrees
// finds the maximal depth starting at a given node recursively
const maxDepth = (node, depth) => {
if (node == null) {
return depth;
} else {
let maxLeft = maxDepth(node.left, depth + 1);
let maxRight = maxDepth(node.right, depth + 1);
return maxLeft > maxRight ? maxLeft : maxRight;
}
};
// checks if a node is balanced recursively
const checkBalanced = node => {
if (node == null) {
return true;
}
let heightLeft = maxDepth(node.left, 0);
let heightRight = maxDepth(node.right, 0);
if (Math.abs(heightLeft - heightRight) <= 1) {
return checkBalanced(node.left) && checkBalanced(node.right);
} else {
return false;
}
};
const isBalanced = root => {
return checkBalanced(root);
}