-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path110.js
More file actions
26 lines (25 loc) · 691 Bytes
/
110.js
File metadata and controls
26 lines (25 loc) · 691 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
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isBalanced = function(root) {
const getHeight = node => {
if (node === null) return 0;
if (node.height) return node.height;
const height = Math.max(getHeight(node.left), getHeight(node.right)) + 1;
node.height = height;
return height;
}
if (root === null) return true;
leftHeight = getHeight(root.left);
rightHeight = getHeight(root.right);
if (Math.abs(leftHeight - rightHeight) > 1) return false;
return isBalanced(root.left) && isBalanced(root.right);
};