Skip to content

Commit e37213b

Browse files
authored
Create depth-of-bst-given-insertion-order.cpp
1 parent b9049ac commit e37213b

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Time: O(nlogn)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
int maxDepthBST(vector<int>& order) {
7+
map<int, int> depths = {{numeric_limits<int>::min(), 0},
8+
{numeric_limits<int>::max(), 0}};
9+
int result = 0;
10+
for (const auto& x : order) {
11+
auto it = depths.upper_bound(x);
12+
depths[x] = max(prev(it)->second, it->second) + 1;
13+
result = max(result, depths[x]);
14+
}
15+
return result;
16+
}
17+
};

0 commit comments

Comments
 (0)