Skip to content

Commit 857d899

Browse files
authored
Update 110._balanced_binary_tree.md
1 parent 01d2982 commit 857d899

File tree

1 file changed

+61
-30
lines changed

1 file changed

+61
-30
lines changed
Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,68 @@
1-
###110. Balanced Binary Tree
2-
3-
题目:
4-
<https://leetcode.com/problems/balanced-binary-tree/>
5-
6-
7-
难度:
8-
Easy
9-
10-
11-
全程递归中
12-
13-
14-
15-
```
1+
# 110. Balanced Binary Tree
2+
**<font color=red>难度: 简单</font>**
3+
4+
## 刷题内容
5+
6+
> 原题连接
7+
8+
* https://leetcode.com/problems/balanced-binary-tree/description/
9+
10+
> 内容描述
11+
12+
```
13+
Given a binary tree, determine if it is height-balanced.
14+
15+
For this problem, a height-balanced binary tree is defined as:
16+
17+
a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
18+
19+
Example 1:
20+
21+
Given the following tree [3,9,20,null,null,15,7]:
22+
23+
3
24+
/ \
25+
9 20
26+
/ \
27+
15 7
28+
Return true.
29+
30+
Example 2:
31+
32+
Given the following tree [1,2,2,3,3,null,null,4,4]:
33+
34+
1
35+
/ \
36+
2 2
37+
/ \
38+
3 3
39+
/ \
40+
4 4
41+
Return false.
42+
```
43+
44+
## 解题方案
45+
46+
> 思路 1
47+
48+
递归,判断左右子树最大高度差不超过1且左右子树均为平衡树
49+
50+
```python
1651
class Solution(object):
1752
def isBalanced(self, root):
1853
"""
1954
:type root: TreeNode
2055
:rtype: bool
2156
"""
22-
if root == None:
57+
def height(node):
58+
if not node:
59+
return 0
60+
return 1 + max(height(node.left), height(node.right))
61+
if not root:
2362
return True
24-
25-
lh = self.height(root.left)
26-
rh = self.height(root.right)
27-
28-
if abs(lh-rh) <= 1 and self.isBalanced(root.left) and self.isBalanced(root.right):
29-
return True
30-
return False
31-
32-
33-
def height(self, node):
34-
if node == None:
35-
return 0
36-
return 1 + max(self.height(node.left),self.height(node.right))
37-
```
63+
return abs(height(root.left) - height(root.right)) <= 1 and self.isBalanced(root.left) and self.isBalanced(root.right)
64+
```
65+
66+
67+
68+

0 commit comments

Comments
 (0)