-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.ts
34 lines (32 loc) · 1.01 KB
/
Solution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function bstFromPreorder(preorder: number[]): TreeNode | null {
const n = preorder.length;
const next = new Array(n);
const stack = [];
for (let i = n - 1; i >= 0; i--) {
while (stack.length !== 0 && preorder[stack[stack.length - 1]] < preorder[i]) {
stack.pop();
}
next[i] = stack[stack.length - 1] ?? n;
stack.push(i);
}
const dfs = (left: number, right: number) => {
if (left >= right) {
return null;
}
return new TreeNode(preorder[left], dfs(left + 1, next[left]), dfs(next[left], right));
};
return dfs(0, n);
}