Skip to content

Commit 4ecb5cf

Browse files
author
zj
committed
110. Balanced Binary Tree
1 parent 3fdab7a commit 4ecb5cf

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# 110. Balanced Binary Tree
2+
Given a binary tree, determine if it is height-balanced.
3+
4+
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {boolean}
11+
*/
12+
var isBalanced = function(root) {
13+
if(root === null) return true;
14+
function depth(root){
15+
if(root === null) return 0;
16+
return Math.max(depth(root.left),depth(root.right))+1
17+
}
18+
var left = depth(root.left);
19+
var right = depth(root.right);
20+
return Math.abs(left - right) <= 1 && isBalanced(root.left) && isBalanced(root.right);
21+
};

0 commit comments

Comments
 (0)