Skip to content

Commit 964a38f

Browse files
committed
feat: add solutions to lc problem: No.0110. Balanced Binary Tree
1 parent 30981f0 commit 964a38f

File tree

5 files changed

+239
-19
lines changed

5 files changed

+239
-19
lines changed

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

+85-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
4747
</ul>
4848

49-
5049
## 解法
5150

5251
<!-- 这里可写通用的实现逻辑 -->
@@ -58,15 +57,99 @@
5857
<!-- 这里可写当前语言的特殊实现逻辑 -->
5958

6059
```python
61-
60+
# Definition for a binary tree node.
61+
# class TreeNode:
62+
# def __init__(self, val=0, left=None, right=None):
63+
# self.val = val
64+
# self.left = left
65+
# self.right = right
66+
class Solution:
67+
def isBalanced(self, root: TreeNode) -> bool:
68+
def height(root):
69+
if not root:
70+
return 0
71+
return 1 + max(height(root.left), height(root.right))
72+
if not root:
73+
return True
74+
left_height, right_height = height(root.left), height(root.right)
75+
return abs(left_height - right_height) <= 1 and self.isBalanced(root.left) and self.isBalanced(root.right)
6276
```
6377

6478
### **Java**
6579

6680
<!-- 这里可写当前语言的特殊实现逻辑 -->
6781

6882
```java
83+
/**
84+
* Definition for a binary tree node.
85+
* public class TreeNode {
86+
* int val;
87+
* TreeNode left;
88+
* TreeNode right;
89+
* TreeNode() {}
90+
* TreeNode(int val) { this.val = val; }
91+
* TreeNode(int val, TreeNode left, TreeNode right) {
92+
* this.val = val;
93+
* this.left = left;
94+
* this.right = right;
95+
* }
96+
* }
97+
*/
98+
class Solution {
99+
public boolean isBalanced(TreeNode root) {
100+
if (root == null) {
101+
return true;
102+
}
103+
int leftHeight = height(root.left);
104+
int rightHeight = height(root.right);
105+
return Math.abs(leftHeight - rightHeight) <= 1 && isBalanced(root.left) && isBalanced(root.right);
106+
}
107+
108+
private int height(TreeNode root) {
109+
if (root == null) {
110+
return 0;
111+
}
112+
int l = height(root.left);
113+
int r = height(root.right);
114+
return 1 + Math.max(l, r);
115+
}
116+
}
117+
```
69118

119+
### **C++**
120+
121+
```cpp
122+
/**
123+
* Definition for a binary tree node.
124+
* struct TreeNode {
125+
* int val;
126+
* TreeNode *left;
127+
* TreeNode *right;
128+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
129+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
130+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
131+
* };
132+
*/
133+
class Solution {
134+
public:
135+
bool isBalanced(TreeNode* root) {
136+
if (root == nullptr) {
137+
return true;
138+
}
139+
int leftHeight = height(root->left);
140+
int rightHeight = height(root->right);
141+
return abs(leftHeight - rightHeight) <= 1 && isBalanced(root->left) && isBalanced(root->right);
142+
}
143+
private:
144+
int height(TreeNode* root) {
145+
if (root == nullptr) {
146+
return 0;
147+
}
148+
int l = height(root->left);
149+
int r = height(root->right);
150+
return 1 + max(l, r);
151+
}
152+
};
70153
```
71154
72155
### **...**

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

+85-2
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,104 @@
4242
<li><code>-10<sup>4</sup> &lt;= Node.val &lt;= 10<sup>4</sup></code></li>
4343
</ul>
4444

45-
4645
## Solutions
4746

4847
<!-- tabs:start -->
4948

5049
### **Python3**
5150

5251
```python
53-
52+
# Definition for a binary tree node.
53+
# class TreeNode:
54+
# def __init__(self, val=0, left=None, right=None):
55+
# self.val = val
56+
# self.left = left
57+
# self.right = right
58+
class Solution:
59+
def isBalanced(self, root: TreeNode) -> bool:
60+
def height(root):
61+
if not root:
62+
return 0
63+
return 1 + max(height(root.left), height(root.right))
64+
if not root:
65+
return True
66+
left_height, right_height = height(root.left), height(root.right)
67+
return abs(left_height - right_height) <= 1 and self.isBalanced(root.left) and self.isBalanced(root.right)
5468
```
5569

5670
### **Java**
5771

5872
```java
73+
/**
74+
* Definition for a binary tree node.
75+
* public class TreeNode {
76+
* int val;
77+
* TreeNode left;
78+
* TreeNode right;
79+
* TreeNode() {}
80+
* TreeNode(int val) { this.val = val; }
81+
* TreeNode(int val, TreeNode left, TreeNode right) {
82+
* this.val = val;
83+
* this.left = left;
84+
* this.right = right;
85+
* }
86+
* }
87+
*/
88+
class Solution {
89+
public boolean isBalanced(TreeNode root) {
90+
if (root == null) {
91+
return true;
92+
}
93+
int leftHeight = height(root.left);
94+
int rightHeight = height(root.right);
95+
return Math.abs(leftHeight - rightHeight) <= 1 && isBalanced(root.left) && isBalanced(root.right);
96+
}
97+
98+
private int height(TreeNode root) {
99+
if (root == null) {
100+
return 0;
101+
}
102+
int l = height(root.left);
103+
int r = height(root.right);
104+
return 1 + Math.max(l, r);
105+
}
106+
}
107+
```
59108

109+
### **C++**
110+
111+
```cpp
112+
/**
113+
* Definition for a binary tree node.
114+
* struct TreeNode {
115+
* int val;
116+
* TreeNode *left;
117+
* TreeNode *right;
118+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
119+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
120+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
121+
* };
122+
*/
123+
class Solution {
124+
public:
125+
bool isBalanced(TreeNode* root) {
126+
if (root == nullptr) {
127+
return true;
128+
}
129+
int leftHeight = height(root->left);
130+
int rightHeight = height(root->right);
131+
return abs(leftHeight - rightHeight) <= 1 && isBalanced(root->left) && isBalanced(root->right);
132+
}
133+
private:
134+
int height(TreeNode* root) {
135+
if (root == nullptr) {
136+
return 0;
137+
}
138+
int l = height(root->left);
139+
int r = height(root->right);
140+
return 1 + max(l, r);
141+
}
142+
};
60143
```
61144
62145
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
112
class Solution {
213
public:
314
bool isBalanced(TreeNode* root) {
4-
if (!root) return true;
5-
if (abs(getDepth(root->left) - getDepth(root->right)) > 1) return false;
6-
return isBalanced(root->left) && isBalanced(root->right);
15+
if (root == nullptr) {
16+
return true;
17+
}
18+
int leftHeight = height(root->left);
19+
int rightHeight = height(root->right);
20+
return abs(leftHeight - rightHeight) <= 1 && isBalanced(root->left) && isBalanced(root->right);
721
}
8-
922
private:
10-
int getDepth(TreeNode *root) {
11-
if (!root) return 0;
12-
return 1 + max(getDepth(root->left), getDepth(root->right));
23+
int height(TreeNode* root) {
24+
if (root == nullptr) {
25+
return 0;
26+
}
27+
int l = height(root->left);
28+
int r = height(root->right);
29+
return 1 + max(l, r);
1330
}
1431
};
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
116
class Solution {
217
public boolean isBalanced(TreeNode root) {
3-
return depth(root) != -1;
18+
if (root == null) {
19+
return true;
20+
}
21+
int leftHeight = height(root.left);
22+
int rightHeight = height(root.right);
23+
return Math.abs(leftHeight - rightHeight) <= 1 && isBalanced(root.left) && isBalanced(root.right);
424
}
5-
private int depth(TreeNode root) {
6-
if (root == null) return 0;
7-
int left = depth(root.left);
8-
if (left == -1) return -1;
9-
int right = depth(root.right);
10-
if (right == -1 || Math.abs(left - right) > 1) return -1;
11-
return Math.max(left, right) + 1;
25+
26+
private int height(TreeNode root) {
27+
if (root == null) {
28+
return 0;
29+
}
30+
int l = height(root.left);
31+
int r = height(root.right);
32+
return 1 + Math.max(l, r);
1233
}
1334
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def isBalanced(self, root: TreeNode) -> bool:
9+
def height(root):
10+
if not root:
11+
return 0
12+
return 1 + max(height(root.left), height(root.right))
13+
if not root:
14+
return True
15+
left_height, right_height = height(root.left), height(root.right)
16+
return abs(left_height - right_height) <= 1 and self.isBalanced(root.left) and self.isBalanced(root.right)

0 commit comments

Comments
 (0)