Skip to content

Commit 2376c07

Browse files
committed
feat: add solutions to lc problem: No.0110
No.0110.Balanced Binary Tree
1 parent 741ba9a commit 2376c07

File tree

6 files changed

+104
-96
lines changed

6 files changed

+104
-96
lines changed

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

+42-32
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53+
**方法一:自底向上的递归**
54+
55+
定义函数 $height(root)$ 计算二叉树的高度,处理逻辑如下:
56+
57+
- 如果二叉树 $root$ 为空,返回 $0$。
58+
- 否则,递归计算左右子树的高度,分别为 $l$ 和 $r$。如果 $l$ 或 $r$ 为 $-1$,或者 $l$ 和 $r$ 的差的绝对值大于 $1$,则返回 $-1$,否则返回 $max(l, r) + 1$。
59+
60+
那么,如果函数 $height(root)$ 返回的是 $-1$,则说明二叉树 $root$ 不是平衡二叉树,否则是平衡二叉树。
61+
62+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点数。
63+
5364
<!-- tabs:start -->
5465

5566
### **Python3**
@@ -64,7 +75,7 @@
6475
# self.left = left
6576
# self.right = right
6677
class Solution:
67-
def isBalanced(self, root: TreeNode) -> bool:
78+
def isBalanced(self, root: Optional[TreeNode]) -> bool:
6879
def height(root):
6980
if root is None:
7081
return 0
@@ -131,8 +142,8 @@ class Solution {
131142
* @return {boolean}
132143
*/
133144
var isBalanced = function (root) {
134-
let height = function (root) {
135-
if (root == null) {
145+
const height = root => {
146+
if (!root) {
136147
return 0;
137148
}
138149
const l = height(root.left);
@@ -142,7 +153,6 @@ var isBalanced = function (root) {
142153
}
143154
return 1 + Math.max(l, r);
144155
};
145-
146156
return height(root) >= 0;
147157
};
148158
```
@@ -164,15 +174,19 @@ var isBalanced = function (root) {
164174
class Solution {
165175
public:
166176
bool isBalanced(TreeNode* root) {
177+
function<int(TreeNode*)> height = [&](TreeNode* root) {
178+
if (!root) {
179+
return 0;
180+
}
181+
int l = height(root->left);
182+
int r = height(root->right);
183+
if (l == -1 || r == -1 || abs(l - r) > 1) {
184+
return -1;
185+
}
186+
return 1 + max(l, r);
187+
};
167188
return height(root) >= 0;
168189
}
169-
170-
int height(TreeNode* root) {
171-
if (!root) return 0;
172-
int l = height(root->left), r = height(root->right);
173-
if (l == -1 || r == -1 || abs(l - r) > 1) return -1;
174-
return 1 + max(l, r);
175-
}
176190
};
177191
```
178192
@@ -188,32 +202,28 @@ public:
188202
* }
189203
*/
190204
func isBalanced(root *TreeNode) bool {
191-
return height(root) >= 0
192-
}
193-
194-
func height(root *TreeNode) int {
195-
if root == nil {
196-
return 0
197-
}
198-
l, r := height(root.Left), height(root.Right)
199-
if l == -1 || r == -1 || abs(l-r) > 1 {
200-
return -1
205+
var height func(*TreeNode) int
206+
height = func(root *TreeNode) int {
207+
if root == nil {
208+
return 0
209+
}
210+
l, r := height(root.Left), height(root.Right)
211+
if l == -1 || r == -1 || abs(l-r) > 1 {
212+
return -1
213+
}
214+
if l > r {
215+
return 1 + l
216+
}
217+
return 1 + r
201218
}
202-
return 1 + max(l, r)
219+
return height(root) >= 0
203220
}
204221
205222
func abs(x int) int {
206-
if x >= 0 {
207-
return x
208-
}
209-
return -x
210-
}
211-
212-
func max(a, b int) int {
213-
if a > b {
214-
return a
223+
if x < 0 {
224+
return -x
215225
}
216-
return b
226+
return x
217227
}
218228
```
219229

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

+31-32
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
# self.left = left
5151
# self.right = right
5252
class Solution:
53-
def isBalanced(self, root: TreeNode) -> bool:
53+
def isBalanced(self, root: Optional[TreeNode]) -> bool:
5454
def height(root):
5555
if root is None:
5656
return 0
@@ -115,8 +115,8 @@ class Solution {
115115
* @return {boolean}
116116
*/
117117
var isBalanced = function (root) {
118-
let height = function (root) {
119-
if (root == null) {
118+
const height = root => {
119+
if (!root) {
120120
return 0;
121121
}
122122
const l = height(root.left);
@@ -126,7 +126,6 @@ var isBalanced = function (root) {
126126
}
127127
return 1 + Math.max(l, r);
128128
};
129-
130129
return height(root) >= 0;
131130
};
132131
```
@@ -148,15 +147,19 @@ var isBalanced = function (root) {
148147
class Solution {
149148
public:
150149
bool isBalanced(TreeNode* root) {
150+
function<int(TreeNode*)> height = [&](TreeNode* root) {
151+
if (!root) {
152+
return 0;
153+
}
154+
int l = height(root->left);
155+
int r = height(root->right);
156+
if (l == -1 || r == -1 || abs(l - r) > 1) {
157+
return -1;
158+
}
159+
return 1 + max(l, r);
160+
};
151161
return height(root) >= 0;
152162
}
153-
154-
int height(TreeNode* root) {
155-
if (!root) return 0;
156-
int l = height(root->left), r = height(root->right);
157-
if (l == -1 || r == -1 || abs(l - r) > 1) return -1;
158-
return 1 + max(l, r);
159-
}
160163
};
161164
```
162165
@@ -172,32 +175,28 @@ public:
172175
* }
173176
*/
174177
func isBalanced(root *TreeNode) bool {
175-
return height(root) >= 0
176-
}
177-
178-
func height(root *TreeNode) int {
179-
if root == nil {
180-
return 0
181-
}
182-
l, r := height(root.Left), height(root.Right)
183-
if l == -1 || r == -1 || abs(l-r) > 1 {
184-
return -1
178+
var height func(*TreeNode) int
179+
height = func(root *TreeNode) int {
180+
if root == nil {
181+
return 0
182+
}
183+
l, r := height(root.Left), height(root.Right)
184+
if l == -1 || r == -1 || abs(l-r) > 1 {
185+
return -1
186+
}
187+
if l > r {
188+
return 1 + l
189+
}
190+
return 1 + r
185191
}
186-
return 1 + max(l, r)
192+
return height(root) >= 0
187193
}
188194
189195
func abs(x int) int {
190-
if x >= 0 {
191-
return x
192-
}
193-
return -x
194-
}
195-
196-
func max(a, b int) int {
197-
if a > b {
198-
return a
196+
if x < 0 {
197+
return -x
199198
}
200-
return b
199+
return x
201200
}
202201
```
203202

solution/0100-0199/0110.Balanced Binary Tree/Solution.cpp

+11-7
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@
1212
class Solution {
1313
public:
1414
bool isBalanced(TreeNode* root) {
15+
function<int(TreeNode*)> height = [&](TreeNode* root) {
16+
if (!root) {
17+
return 0;
18+
}
19+
int l = height(root->left);
20+
int r = height(root->right);
21+
if (l == -1 || r == -1 || abs(l - r) > 1) {
22+
return -1;
23+
}
24+
return 1 + max(l, r);
25+
};
1526
return height(root) >= 0;
1627
}
17-
18-
int height(TreeNode* root) {
19-
if (!root) return 0;
20-
int l = height(root->left), r = height(root->right);
21-
if (l == -1 || r == -1 || abs(l - r) > 1) return -1;
22-
return 1 + max(l, r);
23-
}
2428
};

solution/0100-0199/0110.Balanced Binary Tree/Solution.go

+17-21
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,26 @@
77
* }
88
*/
99
func isBalanced(root *TreeNode) bool {
10-
return height(root) >= 0
11-
}
12-
13-
func height(root *TreeNode) int {
14-
if root == nil {
15-
return 0
16-
}
17-
l, r := height(root.Left), height(root.Right)
18-
if l == -1 || r == -1 || abs(l-r) > 1 {
19-
return -1
10+
var height func(*TreeNode) int
11+
height = func(root *TreeNode) int {
12+
if root == nil {
13+
return 0
14+
}
15+
l, r := height(root.Left), height(root.Right)
16+
if l == -1 || r == -1 || abs(l-r) > 1 {
17+
return -1
18+
}
19+
if l > r {
20+
return 1 + l
21+
}
22+
return 1 + r
2023
}
21-
return 1 + max(l, r)
24+
return height(root) >= 0
2225
}
2326

2427
func abs(x int) int {
25-
if x >= 0 {
26-
return x
27-
}
28-
return -x
29-
}
30-
31-
func max(a, b int) int {
32-
if a > b {
33-
return a
28+
if x < 0 {
29+
return -x
3430
}
35-
return b
31+
return x
3632
}

solution/0100-0199/0110.Balanced Binary Tree/Solution.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
* @return {boolean}
1212
*/
1313
var isBalanced = function (root) {
14-
let height = function (root) {
15-
if (root == null) {
14+
const height = root => {
15+
if (!root) {
1616
return 0;
1717
}
1818
const l = height(root.left);
@@ -22,6 +22,5 @@ var isBalanced = function (root) {
2222
}
2323
return 1 + Math.max(l, r);
2424
};
25-
2625
return height(root) >= 0;
2726
};

solution/0100-0199/0110.Balanced Binary Tree/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# self.left = left
66
# self.right = right
77
class Solution:
8-
def isBalanced(self, root: TreeNode) -> bool:
8+
def isBalanced(self, root: Optional[TreeNode]) -> bool:
99
def height(root):
1010
if root is None:
1111
return 0

0 commit comments

Comments
 (0)