54
54
55
55
class Solution :
56
56
def isBalanced (self , root : TreeNode) -> bool :
57
+ def height (root ):
58
+ if root is None :
59
+ return 0
60
+ return 1 + max (height(root.left), height(root.right))
61
+
57
62
if root is None :
58
63
return True
59
- return abs (self ._height(root.left) - self ._height(root.right)) <= 1 and self .isBalanced(root.left) and self .isBalanced(root.right)
60
-
61
- def _height (self , tree ):
62
- if tree is None :
63
- return 0
64
- return 1 + max (self ._height(tree.left), self ._height(tree.right))
64
+ return abs (height(root.left) - height(root.right)) <= 1 and self .isBalanced(root.left) and self .isBalanced(root.right)
65
65
```
66
66
67
67
### ** Java**
@@ -81,14 +81,14 @@ class Solution {
81
81
if (root == null ) {
82
82
return true ;
83
83
}
84
- return Math . abs(height (root. left) - height (root. right)) <= 1 && isBalanced(root. left) && isBalanced(root. right);
84
+ return Math . abs(depth (root. left) - depth (root. right)) <= 1 && isBalanced(root. left) && isBalanced(root. right);
85
85
}
86
86
87
- private int height (TreeNode tree ) {
87
+ private int depth (TreeNode tree ) {
88
88
if (tree == null ) {
89
89
return 0 ;
90
90
}
91
- return 1 + Math . max(height (tree. left), height (tree. right));
91
+ return 1 + Math . max(depth (tree. left), depth (tree. right));
92
92
}
93
93
}
94
94
```
@@ -107,59 +107,82 @@ class Solution {
107
107
* @param {TreeNode} root
108
108
* @return {boolean}
109
109
*/
110
- var isBalanced = function (root ) {
111
- if (! root) return true ;
112
- if (! isBalanced (root .left ) || ! isBalanced (root .right )) return false ;
113
- if (Math .abs (getDepth (root .left ) - getDepth (root .right )) > 1 ) return false ;
114
- return true ;
115
- };
110
+ var isBalanced = function (root ) {
111
+ const depth = (root ) => {
112
+ if (! root) {
113
+ return 0 ;
114
+ }
115
+ return 1 + Math .max (depth (root .left ), depth (root .right ));
116
+ }
116
117
117
- function getDepth (node ) {
118
- if (! node) return 0 ;
119
- return Math .max (getDepth (node .left ), getDepth (node .right )) + 1 ;
120
- }
118
+ if (! root) {
119
+ return true ;
120
+ }
121
+ return Math .abs (depth (root .left ) - depth (root .right )) <= 1 && isBalanced (root .left ) && isBalanced (root .right );
122
+ };
121
123
```
122
124
123
125
### ** C++**
124
126
125
127
``` cpp
128
+ /* *
129
+ * Definition for a binary tree node.
130
+ * struct TreeNode {
131
+ * int val;
132
+ * TreeNode *left;
133
+ * TreeNode *right;
134
+ * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
135
+ * };
136
+ */
126
137
class Solution {
127
138
public:
128
- bool isBalanced(TreeNode* root, int* depth) {
129
- if (root == nullptr) {
130
- * depth = 0;
139
+ bool isBalanced(TreeNode* root) {
140
+ if (!root) {
131
141
return true;
132
142
}
133
-
134
- int left = 0;
135
- int right = 0;
136
- if (isBalanced(root->left, &left) && isBalanced(root->right, &right)) {
137
- // 这样做的优势是,不用每次都计算单个子树的深度
138
- int diff = left - right;
139
- if (diff > 1 || diff < -1) {
140
- // 如果有一处不符合 -1 < diff < 1,则直接返回false
141
- return false;
142
- } else {
143
- // 如果符合,则记录当前深度,然后返回上一层继续计算
144
- *depth = max(left, right) + 1;
145
- return true;
146
- }
147
- }
148
-
149
- return false ; // 如果
143
+ return abs(depth(root->left) - depth(root->right)) <= 1 && isBalanced(root->left) && isBalanced(root->right);
150
144
}
151
145
152
- bool isBalanced (TreeNode* root) {
146
+ private:
147
+ int depth(TreeNode* root) {
153
148
if (!root) {
154
- return true ;
149
+ return 0 ;
155
150
}
156
-
157
- int depth = 0;
158
- return isBalanced(root, &depth);
151
+ return 1 + max(depth(root->left), depth(root->right));
159
152
}
160
153
};
161
154
```
162
155
156
+ ### **Go**
157
+
158
+ ```go
159
+ /**
160
+ * Definition for a binary tree node.
161
+ * type TreeNode struct {
162
+ * Val int
163
+ * Left *TreeNode
164
+ * Right *TreeNode
165
+ * }
166
+ */
167
+ func isBalanced(root *TreeNode) bool {
168
+ if (root == nil) {
169
+ return true
170
+ }
171
+ return math.Abs(float64(depth(root.Left)-depth(root.Right))) <= 1 && isBalanced(root.Left) && isBalanced(root.Right)
172
+ }
173
+
174
+ func depth(root *TreeNode) int {
175
+ if (root == nil) {
176
+ return 0
177
+ }
178
+ left, right := depth(root.Left), depth(root.Right)
179
+ if (left > right) {
180
+ return 1 + left
181
+ }
182
+ return 1 + right
183
+ }
184
+ ```
185
+
163
186
### ** ...**
164
187
165
188
```
0 commit comments