Skip to content

Commit 0a34570

Browse files
committed
feat: add python and java solutions to lcci question
添加《程序员面试金典》题解:面试题 04.04. 检查平衡性
1 parent d5ddf99 commit 0a34570

File tree

4 files changed

+208
-54
lines changed

4 files changed

+208
-54
lines changed

lcci/04.04.Check Balance/README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,61 @@
66

77
## 解法
88
<!-- 这里可写通用的实现逻辑 -->
9-
9+
递归法。
1010

1111
### Python3
1212
<!-- 这里可写当前语言的特殊实现逻辑 -->
1313

1414
```python
15+
# Definition for a binary tree node.
16+
# class TreeNode:
17+
# def __init__(self, x):
18+
# self.val = x
19+
# self.left = None
20+
# self.right = None
21+
22+
class Solution:
23+
def isBalanced(self, root: TreeNode) -> bool:
24+
if not root:
25+
return True
26+
l, r = self._height(root.left), self._height(root.right)
27+
return abs(l - r) < 2 and self.isBalanced(root.left) and self.isBalanced(root.right)
1528

29+
def _height(self, node):
30+
if not node:
31+
return 0
32+
return 1 + max(self._height(node.left), self._height(node.right))
1633
```
1734

1835
### Java
1936
<!-- 这里可写当前语言的特殊实现逻辑 -->
2037

2138
```java
39+
/**
40+
* Definition for a binary tree node.
41+
* public class TreeNode {
42+
* int val;
43+
* TreeNode left;
44+
* TreeNode right;
45+
* TreeNode(int x) { val = x; }
46+
* }
47+
*/
48+
class Solution {
49+
public boolean isBalanced(TreeNode root) {
50+
if (root == null) {
51+
return true;
52+
}
53+
int l = height(root.left), r = height(root.right);
54+
return Math.abs(l - r) < 2 && isBalanced(root.left) && isBalanced(root.right);
55+
}
2256

57+
private int height(TreeNode node) {
58+
if (node == null) {
59+
return 0;
60+
}
61+
return 1 + Math.max(height(node.left), height(node.right));
62+
}
63+
}
2364
```
2465

2566
### ...

lcci/04.04.Check Balance/README_EN.md

Lines changed: 122 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,122 @@
1-
# [04.04. Check Balance](https://leetcode-cn.com/problems/check-balance-lcci)
2-
3-
## Description
4-
<p>Implement a function to check if a binary tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one.</p>
5-
6-
<p><br />
7-
<strong>Example 1:</strong></p>
8-
9-
<pre>
10-
Given tree [3,9,20,null,null,15,7]
11-
3
12-
/ \
13-
9 20
14-
/ \
15-
15 7
16-
return true.</pre>
17-
18-
<p><strong>Example 2:</strong></p>
19-
20-
<pre>
21-
Given [1,2,2,3,3,null,null,4,4]
22-
1
23-
/ \
24-
2 2
25-
/ \
26-
3 3
27-
/ \
28-
4 4
29-
return&nbsp;false.</pre>
30-
31-
<p>&nbsp;</p>
32-
33-
34-
35-
## Solutions
36-
37-
38-
### Python3
39-
40-
```python
41-
42-
```
43-
44-
### Java
45-
46-
```java
47-
48-
```
49-
50-
### ...
51-
```
52-
53-
```
1+
# [04.04. Check Balance](https://leetcode-cn.com/problems/check-balance-lcci)
2+
3+
## Description
4+
<p>Implement a function to check if a binary tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one.</p>
5+
6+
7+
8+
<p><br />
9+
10+
<strong>Example 1:</strong></p>
11+
12+
13+
14+
<pre>
15+
16+
Given tree [3,9,20,null,null,15,7]
17+
18+
3
19+
20+
/ \
21+
22+
9 20
23+
24+
/ \
25+
26+
15 7
27+
28+
return true.</pre>
29+
30+
31+
32+
<p><strong>Example 2:</strong></p>
33+
34+
35+
36+
<pre>
37+
38+
Given [1,2,2,3,3,null,null,4,4]
39+
40+
1
41+
42+
/ \
43+
44+
2 2
45+
46+
/ \
47+
48+
3 3
49+
50+
/ \
51+
52+
4 4
53+
54+
return&nbsp;false.</pre>
55+
56+
57+
58+
<p>&nbsp;</p>
59+
60+
61+
62+
63+
## Solutions
64+
65+
66+
### Python3
67+
68+
```python
69+
# Definition for a binary tree node.
70+
# class TreeNode:
71+
# def __init__(self, x):
72+
# self.val = x
73+
# self.left = None
74+
# self.right = None
75+
76+
class Solution:
77+
def isBalanced(self, root: TreeNode) -> bool:
78+
if not root:
79+
return True
80+
l, r = self._height(root.left), self._height(root.right)
81+
return abs(l - r) < 2 and self.isBalanced(root.left) and self.isBalanced(root.right)
82+
83+
def _height(self, node):
84+
if not node:
85+
return 0
86+
return 1 + max(self._height(node.left), self._height(node.right))
87+
```
88+
89+
### Java
90+
91+
```java
92+
/**
93+
* Definition for a binary tree node.
94+
* public class TreeNode {
95+
* int val;
96+
* TreeNode left;
97+
* TreeNode right;
98+
* TreeNode(int x) { val = x; }
99+
* }
100+
*/
101+
class Solution {
102+
public boolean isBalanced(TreeNode root) {
103+
if (root == null) {
104+
return true;
105+
}
106+
int l = height(root.left), r = height(root.right);
107+
return Math.abs(l - r) < 2 && isBalanced(root.left) && isBalanced(root.right);
108+
}
109+
110+
private int height(TreeNode node) {
111+
if (node == null) {
112+
return 0;
113+
}
114+
return 1 + Math.max(height(node.left), height(node.right));
115+
}
116+
}
117+
```
118+
119+
### ...
120+
```
121+
122+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public boolean isBalanced(TreeNode root) {
12+
if (root == null) {
13+
return true;
14+
}
15+
int l = height(root.left), r = height(root.right);
16+
return Math.abs(l - r) < 2 && isBalanced(root.left) && isBalanced(root.right);
17+
}
18+
19+
private int height(TreeNode node) {
20+
if (node == null) {
21+
return 0;
22+
}
23+
return 1 + Math.max(height(node.left), height(node.right));
24+
}
25+
}

lcci/04.04.Check Balance/Solution.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
9+
class Solution:
10+
def isBalanced(self, root: TreeNode) -> bool:
11+
if not root:
12+
return True
13+
l, r = self._height(root.left), self._height(root.right)
14+
return abs(l - r) < 2 and self.isBalanced(root.left) and self.isBalanced(root.right)
15+
16+
def _height(self, node):
17+
if not node:
18+
return 0
19+
return 1 + max(self._height(node.left), self._height(node.right))

0 commit comments

Comments
 (0)