Skip to content

Commit 8251bd3

Browse files
committed
Add solution #110
1 parent d621f10 commit 8251bd3

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
101|[Symmetric Tree](./0101-symmetric-tree.js)|Easy|
104104
102|[Binary Tree Level Order Traversal](./0102-binary-tree-level-order-traversal.js)|Medium|
105105
104|[Maximum Depth of Binary Tree](./0104-maximum-depth-of-binary-tree.js)|Easy|
106+
110|[Balanced Binary Tree](./0110-balanced-binary-tree.js)|Easy|
106107
111|[Minimum Depth of Binary Tree](./0111-minimum-depth-of-binary-tree.js)|Easy|
107108
112|[Path Sum](./0112-path-sum.js)|Easy|
108109
116|[Populating Next Right Pointers in Each Node](./0116-populating-next-right-pointers-in-each-node.js)|Medium|
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 110. Balanced Binary Tree
3+
* https://leetcode.com/problems/balanced-binary-tree/
4+
* Difficulty: Easy
5+
*
6+
* Given a binary tree, determine if it is height-balanced.
7+
*/
8+
9+
/**
10+
* Definition for a binary tree node.
11+
* function TreeNode(val, left, right) {
12+
* this.val = (val===undefined ? 0 : val)
13+
* this.left = (left===undefined ? null : left)
14+
* this.right = (right===undefined ? null : right)
15+
* }
16+
*/
17+
/**
18+
* @param {TreeNode} root
19+
* @return {boolean}
20+
*/
21+
var isBalanced = function(root) {
22+
if (!root) return true;
23+
return isBalanced(root.right) && isBalanced(root.left)
24+
&& Math.abs(traverse(root.right) - traverse(root.left)) < 2;
25+
};
26+
27+
function traverse(node, depth = 0) {
28+
if (!node) return depth;
29+
return Math.max(traverse(node.right, depth + 1), traverse(node.left, depth + 1));
30+
}

0 commit comments

Comments
 (0)