@@ -125,9 +125,10 @@ public:
125
125
126
126
1. 明确递归函数的参数和返回值
127
127
128
- 参数的话为传入的节点指针,就没有其他参数需要传递了,返回值要返回传入节点为根节点树的深度。
128
+ 参数:当前传入节点。
129
+ 返回值:以当前传入节点为根节点的树的高度。
129
130
130
- 那么如何标记左右子树是否差值大于1呢。
131
+ 那么如何标记左右子树是否差值大于1呢?
131
132
132
133
如果当前传入节点为根节点的二叉树已经不是二叉平衡树了,还返回高度的话就没有意义了。
133
134
@@ -136,9 +137,9 @@ public:
136
137
代码如下:
137
138
138
139
139
- ```
140
+ ```CPP
140
141
// -1 表示已经不是平衡二叉树了,否则返回值是以该节点为根节点树的高度
141
- int getDepth (TreeNode* node)
142
+ int getHeight (TreeNode* node)
142
143
```
143
144
144
145
2 . 明确终止条件
@@ -147,31 +148,31 @@ int getDepth(TreeNode* node)
147
148
148
149
代码如下:
149
150
150
- ```
151
+ ``` CPP
151
152
if (node == NULL ) {
152
153
return 0;
153
154
}
154
155
```
155
156
156
157
3 . 明确单层递归的逻辑
157
158
158
- 如何判断当前传入节点为根节点的二叉树是否是平衡二叉树呢,当然是左子树高度和右子树高度相差 。
159
+ 如何判断以当前传入节点为根节点的二叉树是否是平衡二叉树呢?当然是其左子树高度和其右子树高度的差值 。
159
160
160
- 分别求出左右子树的高度 ,然后如果差值小于等于1,则返回当前二叉树的高度,否则则返回-1,表示已经不是二叉树了 。
161
+ 分别求出其左右子树的高度 ,然后如果差值小于等于1,则返回当前二叉树的高度,否则则返回-1,表示已经不是二叉平衡树了 。
161
162
162
163
代码如下:
163
164
164
165
``` CPP
165
- int leftDepth = depth (node->left); // 左
166
- if (leftDepth == -1) return -1;
167
- int rightDepth = depth (node->right); // 右
168
- if (rightDepth == -1) return -1;
166
+ int leftHeight = getHeight (node->left); // 左
167
+ if (leftHeight == -1 ) return -1 ;
168
+ int rightHeight = getHeight (node->right); // 右
169
+ if (rightHeight == -1 ) return -1 ;
169
170
170
171
int result;
171
- if (abs(leftDepth - rightDepth ) > 1) { // 中
172
+ if (abs(leftHeight - rightHeight ) > 1 ) { // 中
172
173
result = -1;
173
174
} else {
174
- result = 1 + max(leftDepth, rightDepth ); // 以当前节点为根节点的最大高度
175
+ result = 1 + max(leftHeight, rightHeight ); // 以当前节点为根节点的树的最大高度
175
176
}
176
177
177
178
return result;
@@ -180,27 +181,27 @@ return result;
180
181
代码精简之后如下:
181
182
182
183
``` CPP
183
- int leftDepth = getDepth (node->left);
184
- if (leftDepth == -1 ) return -1 ;
185
- int rightDepth = getDepth (node->right);
186
- if (rightDepth == -1 ) return -1 ;
187
- return abs(leftDepth - rightDepth ) > 1 ? -1 : 1 + max(leftDepth, rightDepth );
184
+ int leftHeight = getHeight (node->left);
185
+ if (leftHeight == -1 ) return -1 ;
186
+ int rightHeight = getHeight (node->right);
187
+ if (rightHeight == -1 ) return -1 ;
188
+ return abs(leftHeight - rightHeight ) > 1 ? -1 : 1 + max(leftHeight, rightHeight );
188
189
```
189
190
190
191
此时递归的函数就已经写出来了,这个递归的函数传入节点指针,返回以该节点为根节点的二叉树的高度,如果不是二叉平衡树,则返回-1。
191
192
192
- getDepth整体代码如下 :
193
+ getHeight整体代码如下 :
193
194
194
195
``` CPP
195
- int getDepth (TreeNode* node) {
196
+ int getHeight (TreeNode* node) {
196
197
if (node == NULL) {
197
198
return 0;
198
199
}
199
- int leftDepth = getDepth (node->left);
200
- if (leftDepth == -1) return -1;
201
- int rightDepth = getDepth (node->right);
202
- if (rightDepth == -1) return -1;
203
- return abs(leftDepth - rightDepth ) > 1 ? -1 : 1 + max(leftDepth, rightDepth );
200
+ int leftHeight = getHeight (node->left);
201
+ if (leftHeight == -1) return -1;
202
+ int rightHeight = getHeight (node->right);
203
+ if (rightHeight == -1) return -1;
204
+ return abs(leftHeight - rightHeight ) > 1 ? -1 : 1 + max(leftHeight, rightHeight );
204
205
}
205
206
```
206
207
@@ -210,18 +211,18 @@ int getDepth(TreeNode* node) {
210
211
class Solution {
211
212
public:
212
213
// 返回以该节点为根节点的二叉树的高度,如果不是二叉搜索树了则返回-1
213
- int getDepth (TreeNode* node) {
214
+ int getHeight (TreeNode* node) {
214
215
if (node == NULL) {
215
216
return 0;
216
217
}
217
- int leftDepth = getDepth (node->left);
218
- if (leftDepth == -1) return -1; // 说明左子树已经不是二叉平衡树
219
- int rightDepth = getDepth (node->right);
220
- if (rightDepth == -1) return -1; // 说明右子树已经不是二叉平衡树
221
- return abs(leftDepth - rightDepth ) > 1 ? -1 : 1 + max(leftDepth, rightDepth );
218
+ int leftHeight = getHeight (node->left);
219
+ if (leftHeight == -1) return -1;
220
+ int rightHeight = getHeight (node->right);
221
+ if (rightHeight == -1) return -1;
222
+ return abs(leftHeight - rightHeight ) > 1 ? -1 : 1 + max(leftHeight, rightHeight );
222
223
}
223
224
bool isBalanced(TreeNode* root) {
224
- return getDepth (root) == -1 ? false : true;
225
+ return getHeight (root) == -1 ? false : true;
225
226
}
226
227
};
227
228
```
@@ -498,20 +499,35 @@ class Solution {
498
499
## Python
499
500
500
501
递归法:
501
- ``` python
502
+ ``` python3
503
+ # Definition for a binary tree node.
504
+ # class TreeNode:
505
+ # def __init__(self, val=0, left=None, right=None):
506
+ # self.val = val
507
+ # self.left = left
508
+ # self.right = right
502
509
class Solution :
503
510
def isBalanced (self , root : TreeNode) -> bool :
504
- return True if self .getDepth(root) != - 1 else False
511
+ if self .get_height(root) != - 1 :
512
+ return True
513
+ else :
514
+ return False
505
515
506
- # 返回以该节点为根节点的二叉树的高度,如果不是二叉搜索树了则返回-1
507
- def getDepth ( self , node ):
508
- if not node :
516
+ def get_height ( self , root : TreeNode) -> int :
517
+ # Base Case
518
+ if not root :
509
519
return 0
510
- leftDepth = self .getDepth(node.left)
511
- if leftDepth == - 1 : return - 1 # 说明左子树已经不是二叉平衡树
512
- rightDepth = self .getDepth(node.right)
513
- if rightDepth == - 1 : return - 1 # 说明右子树已经不是二叉平衡树
514
- return - 1 if abs (leftDepth - rightDepth)> 1 else 1 + max (leftDepth, rightDepth)
520
+ # 左
521
+ if (left_height := self .get_height(root.left)) == - 1 :
522
+ return - 1
523
+ # 右
524
+ if (right_height := self .get_height(root.right)) == - 1 :
525
+ return - 1
526
+ # 中
527
+ if abs (left_height - right_height) > 1 :
528
+ return - 1
529
+ else :
530
+ return 1 + max (left_height, right_height)
515
531
```
516
532
517
533
迭代法:
0 commit comments