forked from JoshCrozier/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0110-balanced-binary-tree.js
47 lines (39 loc) · 1.17 KB
/
0110-balanced-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
/**
* 110. Balanced Binary Tree
* https://leetcode.com/problems/balanced-binary-tree/
* Difficulty: Easy
*
* Given a binary tree, determine if it is height-balanced.
*/
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isBalanced = function(root) {
if (!root) return true;
return isBalanced(root.right) && isBalanced(root.left)
&& Math.abs(traverse(root.right) - traverse(root.left)) < 2;
};
function traverse(node, depth = 0) {
if (!node) return depth;
return Math.max(traverse(node.right, depth + 1), traverse(node.left, depth + 1));
}
// function isBalanced(root) {
// function height(node) {
// if (!node) return 0;
// return 1 + Math.max(height(node.left), height(node.right));
// }
// if (!root) return true;
// let leftH = height(root.left);
// let rightH = height(root.right);
// if (Math.abs(leftH - rightH) > 1) return false;
// return isBalanced(root.left) && isBalanced(root.right);
// }