Skip to content

Commit 0ff89f0

Browse files
authored
Update 0110.平衡二叉树.md
纠正笔误深度和高度
1 parent 6f65bb7 commit 0ff89f0

File tree

1 file changed

+58
-42
lines changed

1 file changed

+58
-42
lines changed

problems/0110.平衡二叉树.md

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,10 @@ public:
125125
126126
1. 明确递归函数的参数和返回值
127127
128-
参数的话为传入的节点指针,就没有其他参数需要传递了,返回值要返回传入节点为根节点树的深度。
128+
参数:当前传入节点。
129+
返回值:以当前传入节点为根节点的树的高度。
129130
130-
那么如何标记左右子树是否差值大于1呢
131+
那么如何标记左右子树是否差值大于1呢
131132
132133
如果当前传入节点为根节点的二叉树已经不是二叉平衡树了,还返回高度的话就没有意义了。
133134
@@ -136,9 +137,9 @@ public:
136137
代码如下:
137138
138139
139-
```
140+
```CPP
140141
// -1 表示已经不是平衡二叉树了,否则返回值是以该节点为根节点树的高度
141-
int getDepth(TreeNode* node)
142+
int getHeight(TreeNode* node)
142143
```
143144

144145
2. 明确终止条件
@@ -147,31 +148,31 @@ int getDepth(TreeNode* node)
147148

148149
代码如下:
149150

150-
```
151+
```CPP
151152
if (node == NULL) {
152153
return 0;
153154
}
154155
```
155156

156157
3. 明确单层递归的逻辑
157158

158-
如何判断当前传入节点为根节点的二叉树是否是平衡二叉树呢,当然是左子树高度和右子树高度相差
159+
如何判断以当前传入节点为根节点的二叉树是否是平衡二叉树呢?当然是其左子树高度和其右子树高度的差值
159160

160-
分别求出左右子树的高度,然后如果差值小于等于1,则返回当前二叉树的高度,否则则返回-1,表示已经不是二叉树了
161+
分别求出其左右子树的高度,然后如果差值小于等于1,则返回当前二叉树的高度,否则则返回-1,表示已经不是二叉平衡树了
161162

162163
代码如下:
163164

164165
```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;
169170

170171
int result;
171-
if (abs(leftDepth - rightDepth) > 1) { // 中
172+
if (abs(leftHeight - rightHeight) > 1) { // 中
172173
result = -1;
173174
} else {
174-
result = 1 + max(leftDepth, rightDepth); // 以当前节点为根节点的最大高度
175+
result = 1 + max(leftHeight, rightHeight); // 以当前节点为根节点的树的最大高度
175176
}
176177

177178
return result;
@@ -180,27 +181,27 @@ return result;
180181
代码精简之后如下:
181182

182183
```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);
188189
```
189190

190191
此时递归的函数就已经写出来了,这个递归的函数传入节点指针,返回以该节点为根节点的二叉树的高度,如果不是二叉平衡树,则返回-1。
191192

192-
getDepth整体代码如下
193+
getHeight整体代码如下
193194

194195
```CPP
195-
int getDepth(TreeNode* node) {
196+
int getHeight(TreeNode* node) {
196197
if (node == NULL) {
197198
return 0;
198199
}
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);
204205
}
205206
```
206207
@@ -210,18 +211,18 @@ int getDepth(TreeNode* node) {
210211
class Solution {
211212
public:
212213
// 返回以该节点为根节点的二叉树的高度,如果不是二叉搜索树了则返回-1
213-
int getDepth(TreeNode* node) {
214+
int getHeight(TreeNode* node) {
214215
if (node == NULL) {
215216
return 0;
216217
}
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);
222223
}
223224
bool isBalanced(TreeNode* root) {
224-
return getDepth(root) == -1 ? false : true;
225+
return getHeight(root) == -1 ? false : true;
225226
}
226227
};
227228
```
@@ -498,20 +499,35 @@ class Solution {
498499
## Python
499500

500501
递归法:
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
502509
class Solution:
503510
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
505515

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:
509519
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)
515531
```
516532

517533
迭代法:

0 commit comments

Comments
 (0)