You are given a 0-indexed integer array order of length n, a
permutation of integers from 1 to n representing the order of insertion into a binary search tree.
A binary search tree is defined as follows:
The left subtree of a node contains only nodes with keys less than the node’s key.
The right subtree of a node contains only nodes with keys greater than the node’s key.
Both the left and right subtrees must also be binary search trees.
The binary search tree is constructed as follows:
order[0] will be the root of the binary search tree.
All subsequent elements are inserted as the child of any existing node such that the binary search tree properties hold.
Return thedepth of the binary search tree.
A binary tree’s depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Input: order =[2,1,4,3]Output: 3Explanation: The binary search tree has a depth of 3with path 2->3->4.
Example 2:
1
2
3
4

Input: order =[2,1,3,4]Output: 3Explanation: The binary search tree has a depth of 3with path 2->3->4.
Example 3:
1
2
3
4

Input: order =[1,2,3,4]Output: 4Explanation: The binary search tree has a depth of 4with path 1->2->3->4.
Constraints:
n == order.length
1 <= n <= 10^5
order is a permutation of integers between 1 and n.
To efficiently compute the depth of the BST as we insert each value, we can use an ordered set or map to keep track of inserted values and their depths. For each new value, its depth is one more than the maximum depth of its predecessor or successor (if they exist). The answer is the maximum depth after all insertions.
#include<map>classSolution {
public:int bstDepth(vector<int>& order) {
map<int, int> mp;
int ans =0;
for (int x : order) {
auto it = mp.lower_bound(x);
int d =1;
if (it != mp.end()) d = max(d, it->second +1);
if (it != mp.begin()) {
--it;
d = max(d, it->second +1);
}
mp[x] = d;
ans = max(ans, d);
}
return ans;
}
};
import"sort"funcbstDepth(order []int) int {
typepairstruct{v, dint}
arr:= []pair{}
ans:=0for_, x:=rangeorder {
i:=sort.Search(len(arr), func(iint) bool { returnarr[i].v>=x })
d:=1ifi < len(arr) &&arr[i].v > x {
d = max(d, arr[i].d+1)
}
ifi > 0 {
d = max(d, arr[i-1].d+1)
}
arr = append(arr, pair{x, d})
sort.Slice(arr, func(i, jint) bool { returnarr[i].v < arr[j].v })
ifd > ans {
ans = d }
}
returnans}
func max(a, bint) int { ifa > b { returna } else { returnb } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.*;
classSolution {
publicintbstDepth(int[] order) {
TreeMap<Integer, Integer> mp =new TreeMap<>();
int ans = 0;
for (int x : order) {
Integer pred = mp.lowerKey(x);
Integer succ = mp.higherKey(x);
int d = 1;
if (succ !=null) d = Math.max(d, mp.get(succ) + 1);
if (pred !=null) d = Math.max(d, mp.get(pred) + 1);
mp.put(x, d);
ans = Math.max(ans, d);
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.TreeMap
classSolution {
funbstDepth(order: IntArray): Int {
val mp = TreeMap<Int, Int>()
var ans = 0for (x in order) {
val pred = mp.lowerKey(x)
val succ = mp.higherKey(x)
var d = 1if (succ !=null) d = maxOf(d, mp[succ]!! + 1)
if (pred !=null) d = maxOf(d, mp[pred]!! + 1)
mp[x] = d
ans = maxOf(ans, d)
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from sortedcontainers import SortedDict
classSolution:
defbstDepth(self, order: list[int]) -> int:
mp = SortedDict()
ans =0for x in order:
idx = mp.bisect_left(x)
d =1if idx < len(mp):
d = max(d, mp.values()[idx] +1)
if idx >0:
d = max(d, mp.values()[idx-1] +1)
mp[x] = d
ans = max(ans, d)
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
use std::collections::BTreeMap;
impl Solution {
pubfnbst_depth(order: Vec<i32>) -> i32 {
letmut mp = BTreeMap::new();
letmut ans =0;
for&x in&order {
letmut d =1;
iflet Some((&succ, &ds)) = mp.range(x+1..).next() {
d = d.max(ds +1);
}
iflet Some((&pred, &dp)) = mp.range(..x).next_back() {
d = d.max(dp +1);
}
mp.insert(x, d);
ans = ans.max(d);
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
bstDepth(order: number[]):number {
constarr: [number, number][] = [];
letans=0;
for (constxoforder) {
leti=0;
while (i<arr.length&&arr[i][0] <x) i++;
letd=1;
if (i<arr.length) d= Math.max(d, arr[i][1] +1);
if (i>0) d= Math.max(d, arr[i-1][1] +1);
arr.push([x, d]);
arr.sort((a, b) =>a[0] -b[0]);
ans= Math.max(ans, d);
}
returnans;
}
}