Skip to content

Commit f60e625

Browse files
committed
update
1 parent 737a1b6 commit f60e625

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
***输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果。如果是则返回 true,否则返回 false。假设输入的数组的任意两个数字都互不相同。***
2+
3+
![algo36](./images/algo36.jpg)
4+
5+
```
6+
class Solution:
7+
def verifyPostorder(self, postorder: List[int]) -> bool:
8+
n = len(postorder)
9+
if n<=1:
10+
return True
11+
root = postorder[-1]
12+
for i in range(n):
13+
if postorder[i] > root:
14+
break
15+
#找到左右子树对应区间
16+
left,right = postorder[:i],postorder[i:-1]
17+
for k in right:
18+
if k < root:
19+
return False
20+
return self.verifyPostorder(left) and self.verifyPostorder(right)
21+
```

images/algo36.jpg

8.89 KB
Loading

0 commit comments

Comments
 (0)