Skip to content

Commit 6adfd15

Browse files
committedApr 6, 2022
feat: add ts solution to lcof problem: No.33
面试题33. 二叉搜索树的后序遍历序列
1 parent b38c5e9 commit 6adfd15

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
 

‎lcof/面试题33. 二叉搜索树的后序遍历序列/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,31 @@ public:
171171
};
172172
```
173173

174+
### **TypeScript**
175+
176+
```ts
177+
function verifyPostorder(postorder: number[]): boolean {
178+
const dfs = (start: number, end: number, maxVal: number) => {
179+
if (start > end) {
180+
return true;
181+
}
182+
const rootVal = postorder[end];
183+
for (let i = end; i >= start; i--) {
184+
const val = postorder[i];
185+
if (val > maxVal) {
186+
return false;
187+
}
188+
if (val < rootVal) {
189+
return dfs(start, i, rootVal) && dfs(i + 1, end - 1, maxVal);
190+
}
191+
}
192+
return dfs(start, end - 1, maxVal);
193+
};
194+
const n = postorder.length;
195+
return dfs(0, n - 1, Infinity);
196+
}
197+
```
198+
174199
### **...**
175200

176201
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function verifyPostorder(postorder: number[]): boolean {
2+
const dfs = (start: number, end: number, maxVal: number) => {
3+
if (start > end) {
4+
return true;
5+
}
6+
const rootVal = postorder[end];
7+
for (let i = end; i >= start; i--) {
8+
const val = postorder[i];
9+
if (val > maxVal) {
10+
return false;
11+
}
12+
if (val < rootVal) {
13+
return dfs(start, i, rootVal) && dfs(i + 1, end - 1, maxVal);
14+
}
15+
}
16+
return dfs(start, end - 1, maxVal);
17+
};
18+
const n = postorder.length;
19+
return dfs(0, n - 1, Infinity);
20+
}

0 commit comments

Comments
 (0)
Please sign in to comment.