Skip to content

Commit c6c14e9

Browse files
committed
feat: add python and java solutions to lcof question
添加《剑指 Offer》题解:面试题33. 二叉搜索树的后序遍历序列
1 parent 1bd7993 commit c6c14e9

File tree

7 files changed

+170
-28
lines changed

7 files changed

+170
-28
lines changed

lcci/17.16.The Masseuse/README.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
## 解法
3434
<!-- 这里可写通用的实现逻辑 -->
35-
动态规划求解
35+
递推求解
3636

3737

3838
### Python3
@@ -46,11 +46,12 @@ class Solution:
4646
n = len(nums)
4747
if n < 2:
4848
return nums[0]
49-
dp = [0 for _ in range(n)]
50-
dp[0], dp[1] = nums[0], max(nums[0], nums[1])
49+
a, b = nums[0], max(nums[0], nums[1])
50+
res = b
5151
for i in range(2, n):
52-
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])
53-
return dp[n - 1]
52+
res = max(a + nums[i], b)
53+
a, b = b, res
54+
return res
5455
```
5556

5657
### Java
@@ -66,13 +67,14 @@ class Solution {
6667
if (n < 2) {
6768
return n == 0 ? 0 : nums[0];
6869
}
69-
int[] dp = new int[n];
70-
dp[0] = nums[0];
71-
dp[1] = Math.max(nums[0], nums[1]);
70+
int a = nums[0], b = Math.max(nums[0], nums[1]);
71+
int res = b;
7272
for (int i = 2; i < n; ++i) {
73-
dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
73+
res = Math.max(a + nums[i], b);
74+
a = b;
75+
b = res;
7476
}
75-
return dp[n - 1];
77+
return res;
7678
}
7779
}
7880
```

lcci/17.16.The Masseuse/README_EN.md

+11-9
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,12 @@ class Solution:
7575
n = len(nums)
7676
if n < 2:
7777
return nums[0]
78-
dp = [0 for _ in range(n)]
79-
dp[0], dp[1] = nums[0], max(nums[0], nums[1])
78+
a, b = nums[0], max(nums[0], nums[1])
79+
res = b
8080
for i in range(2, n):
81-
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])
82-
return dp[n - 1]
81+
res = max(a + nums[i], b)
82+
a, b = b, res
83+
return res
8384
```
8485

8586
### Java
@@ -94,13 +95,14 @@ class Solution {
9495
if (n < 2) {
9596
return n == 0 ? 0 : nums[0];
9697
}
97-
int[] dp = new int[n];
98-
dp[0] = nums[0];
99-
dp[1] = Math.max(nums[0], nums[1]);
98+
int a = nums[0], b = Math.max(nums[0], nums[1]);
99+
int res = b;
100100
for (int i = 2; i < n; ++i) {
101-
dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
101+
res = Math.max(a + nums[i], b);
102+
a = b;
103+
b = res;
102104
}
103-
return dp[n - 1];
105+
return res;
104106
}
105107
}
106108
```

lcci/17.16.The Masseuse/Solution.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ public int massage(int[] nums) {
77
if (n < 2) {
88
return n == 0 ? 0 : nums[0];
99
}
10-
int[] dp = new int[n];
11-
dp[0] = nums[0];
12-
dp[1] = Math.max(nums[0], nums[1]);
10+
int a = nums[0], b = Math.max(nums[0], nums[1]);
11+
int res = b;
1312
for (int i = 2; i < n; ++i) {
14-
dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
13+
res = Math.max(a + nums[i], b);
14+
a = b;
15+
b = res;
1516
}
16-
return dp[n - 1];
17+
return res;
1718
}
1819
}

lcci/17.16.The Masseuse/Solution.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ def massage(self, nums: List[int]) -> int:
55
n = len(nums)
66
if n < 2:
77
return nums[0]
8-
dp = [0 for _ in range(n)]
9-
dp[0], dp[1] = nums[0], max(nums[0], nums[1])
8+
a, b = nums[0], max(nums[0], nums[1])
9+
res = b
1010
for i in range(2, n):
11-
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])
12-
return dp[n - 1]
11+
res = max(a + nums[i], b)
12+
a, b = b, res
13+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# [面试题33. 二叉搜索树的后序遍历序列](https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-hou-xu-bian-li-xu-lie-lcof/)
2+
3+
## 题目描述
4+
<!-- 这里写题目描述 -->
5+
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果。如果是则返回 `true`,否则返回 `false`。假设输入的数组的任意两个数字都互不相同。
6+
7+
参考以下这颗二叉搜索树:
8+
9+
```
10+
5
11+
/ \
12+
2 6
13+
/ \
14+
1 3
15+
```
16+
17+
**示例 1:**
18+
19+
```
20+
输入: [1,6,3,2,5]
21+
输出: false
22+
```
23+
24+
**示例 2:**
25+
26+
```
27+
输入: [1,3,2,6,5]
28+
输出: true
29+
```
30+
31+
**提示:**
32+
33+
- `数组长度 <= 1000`
34+
35+
## 解法
36+
<!-- 这里可写通用的实现逻辑 -->
37+
二叉搜索树的后序遍历序列是 `[左子树, 右子树, 根结点]`,且左子树结点值均小于根结点,右子树结点值均大于根结点,递归判断即可。
38+
39+
40+
### Python3
41+
<!-- 这里可写当前语言的特殊实现逻辑 -->
42+
43+
```python
44+
class Solution:
45+
def verifyPostorder(self, postorder: List[int]) -> bool:
46+
n = len(postorder)
47+
if n < 2:
48+
return True
49+
for i in range(n):
50+
if postorder[i] > postorder[-1]:
51+
break
52+
for j in range(i + 1, n - 1):
53+
if postorder[j] < postorder[-1]:
54+
return False
55+
return (i == 0 or self.verifyPostorder(postorder[:i])) and (i == n - 1 or self.verifyPostorder(postorder[i:-1]))
56+
57+
```
58+
59+
### Java
60+
<!-- 这里可写当前语言的特殊实现逻辑 -->
61+
62+
```java
63+
class Solution {
64+
public boolean verifyPostorder(int[] postorder) {
65+
if (postorder.length == 0) {
66+
return true;
67+
}
68+
return verifyPostorder(postorder, 0, postorder.length - 1);
69+
}
70+
71+
private boolean verifyPostorder(int[] postorder, int from, int to) {
72+
if (from == to) {
73+
return true;
74+
}
75+
int i = from, j = from;
76+
for (; i < to; ++i) {
77+
if (postorder[i] > postorder[to]) {
78+
break;
79+
}
80+
}
81+
for (j = i + 1; j < to; ++j) {
82+
if (postorder[j] < postorder[to]) {
83+
return false;
84+
}
85+
}
86+
return (i == from || verifyPostorder(postorder, from, i - 1)) && (i == to || verifyPostorder(postorder, i, to - 1));
87+
88+
89+
}
90+
}
91+
```
92+
93+
### ...
94+
```
95+
96+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public boolean verifyPostorder(int[] postorder) {
3+
if (postorder.length == 0) {
4+
return true;
5+
}
6+
return verifyPostorder(postorder, 0, postorder.length - 1);
7+
}
8+
9+
private boolean verifyPostorder(int[] postorder, int from, int to) {
10+
if (from == to) {
11+
return true;
12+
}
13+
int i = from, j = from;
14+
for (; i < to; ++i) {
15+
if (postorder[i] > postorder[to]) {
16+
break;
17+
}
18+
}
19+
for (j = i + 1; j < to; ++j) {
20+
if (postorder[j] < postorder[to]) {
21+
return false;
22+
}
23+
}
24+
return (i == from || verifyPostorder(postorder, from, i - 1))
25+
&& (i == to || verifyPostorder(postorder, i, to - 1));
26+
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def verifyPostorder(self, postorder: List[int]) -> bool:
3+
n = len(postorder)
4+
if n < 2:
5+
return True
6+
for i in range(n):
7+
if postorder[i] > postorder[-1]:
8+
break
9+
for j in range(i + 1, n - 1):
10+
if postorder[j] < postorder[-1]:
11+
return False
12+
return (i == 0 or self.verifyPostorder(postorder[:i])) and (i == n - 1 or self.verifyPostorder(postorder[i:-1]))

0 commit comments

Comments
 (0)