fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define pb push_back
  4. #define MOD 1000000007
  5. #define PI 4 * atan(1)
  6. #define sz(A) (int)A.size()
  7. typedef long long ll;
  8. typedef vector<int> vi;
  9. typedef pair<int, int> pii;
  10. typedef vector<long long> vll;
  11. typedef long int int32;
  12. typedef unsigned long int uint32;
  13. typedef long long int int64;
  14. typedef unsigned long long int uint64;
  15.  
  16. struct node{
  17. int val;
  18. node *l, *r;
  19. };
  20. node *newnode(int d){
  21. node* n = new node();
  22. n->val = d;
  23. n->l = n->r = NULL;
  24. return n;
  25. }
  26. void insert(node* &root, int d){
  27. if(root == NULL){
  28. root = newnode(d);
  29. return;
  30. }
  31. if(root->val > d) insert(root->l, d);
  32. else insert(root->r, d);
  33. }
  34. int depth(node *root){
  35. if(root == NULL) return 0;
  36. return max(depth(root->l) + 1, depth(root->r) + 1);
  37. }
  38. inline void solve(int test){
  39. int n,x; cin >> n;
  40. node *root = NULL;
  41. for(int i=0; i<n; i++){
  42. cin >> x;
  43. insert(root, x);
  44. }
  45. cout << depth(root) - 1 << "\n";
  46.  
  47. }
  48. int main(){
  49. ios_base::sync_with_stdio(false);
  50. cin.tie(NULL);
  51. cout.tie(NULL);
  52. int typetest = 1;
  53. if (typetest){
  54. int t;
  55. cin >> t;
  56. cin.ignore();
  57. for(int i=1; i<=t; i++){
  58. solve(i);
  59. }
  60. }
  61. else solve(0);
  62. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty