Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct node
  5. {
  6.     int data;
  7.     int left;
  8.     int right;
  9. };
  10.  
  11. int t_depth(node* arr, int root)
  12. {
  13.     if (root<=-1) return 0;
  14.  
  15.     int left_branch = t_depth(arr, arr[root].right);
  16.     int right_branch = t_depth(arr, arr[root].left);
  17.  
  18.     if (left_branch > right_branch) return (left_branch + 1);
  19.     else return (right_branch + 1);
  20.  
  21. }
  22.  
  23.  
  24. int main()
  25. {
  26.     ios_base::sync_with_stdio(false);
  27.     cin.tie(NULL);
  28.     freopen("height.in", "r", stdin);
  29.     freopen("height.out", "w", stdout);
  30.     int i;
  31.     cin >> i;
  32.     node* bin_tree = new node[i];
  33.     for (int j = 0; j < i; j++)
  34.     {
  35.         cin >> bin_tree[j].data >> bin_tree[j].left >> bin_tree[j].right;
  36.         bin_tree[j].right--;
  37.         bin_tree[j].left--;
  38.     }
  39.  
  40.     if (i !=0 ) cout << t_depth(bin_tree, 0);
  41.  
  42.     else cout << 0;
  43.  
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement