Skip to content

Commit 324e8d8

Browse files
authored
feat: add javascript solution to lc problem: No.0110.Balanced Binary Tree (#405)
1 parent 0aa1ea5 commit 324e8d8

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

solution/0100-0199/0110.Balanced Binary Tree/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,34 @@ class Solution {
116116
}
117117
```
118118

119+
### **JavaScript**
120+
121+
```js
122+
/**
123+
* Definition for a binary tree node.
124+
* function TreeNode(val, left, right) {
125+
* this.val = (val===undefined ? 0 : val)
126+
* this.left = (left===undefined ? null : left)
127+
* this.right = (right===undefined ? null : right)
128+
* }
129+
*/
130+
/**
131+
* @param {TreeNode} root
132+
* @return {boolean}
133+
*/
134+
var isBalanced = function(root) {
135+
if (root == null) return true;
136+
let left = root.left;
137+
let right = root.right;
138+
return isBalanced(left) && isBalanced(right) && Math.abs(depth(left) - depth(right)) <= 1;
139+
};
140+
141+
function depth(root) {
142+
if (root == null) return 0;
143+
return Math.max(depth(root.left), depth(root.right)) + 1;
144+
}
145+
```
146+
119147
### **C++**
120148

121149
```cpp

solution/0100-0199/0110.Balanced Binary Tree/README_EN.md

+28
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,34 @@ class Solution {
106106
}
107107
```
108108

109+
### **JavaScript**
110+
111+
```js
112+
/**
113+
* Definition for a binary tree node.
114+
* function TreeNode(val, left, right) {
115+
* this.val = (val===undefined ? 0 : val)
116+
* this.left = (left===undefined ? null : left)
117+
* this.right = (right===undefined ? null : right)
118+
* }
119+
*/
120+
/**
121+
* @param {TreeNode} root
122+
* @return {boolean}
123+
*/
124+
var isBalanced = function(root) {
125+
if (root == null) return true;
126+
let left = root.left;
127+
let right = root.right;
128+
return isBalanced(left) && isBalanced(right) && Math.abs(depth(left) - depth(right)) <= 1;
129+
};
130+
131+
function depth(root) {
132+
if (root == null) return 0;
133+
return Math.max(depth(root.left), depth(root.right)) + 1;
134+
}
135+
```
136+
109137
### **C++**
110138

111139
```cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {boolean}
12+
*/
13+
var isBalanced = function(root) {
14+
if (root == null) return true;
15+
let left = root.left;
16+
let right = root.right;
17+
return isBalanced(left) && isBalanced(right) && Math.abs(depth(left) - depth(right)) <= 1;
18+
};
19+
20+
function depth(root) {
21+
if (root == null) return 0;
22+
return Math.max(depth(root.left), depth(root.right)) + 1;
23+
}

0 commit comments

Comments
 (0)