Advertisement
Guest User

trasn

a guest
Dec 11th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. int N;
  4. struct Node {
  5. public:
  6. int K;
  7. int L;
  8. int R;
  9. };
  10.  
  11. int height(Node tree[],int i)
  12. {
  13. int h1 = 0, h2 = 0;
  14. if (tree[i].L != 0) {
  15. h1 = height(tree,tree[i].L - 1);
  16. h1++;
  17. }
  18. if (tree[i].R !=0 ) {
  19. h2 = height(tree,tree[i].R - 1);
  20. h2++;
  21. }
  22. int ret=max(h1,h2);
  23. return ret;
  24. }
  25.  
  26. int main() {
  27. ios_base::sync_with_stdio(false);
  28. cin.tie(nullptr);
  29. cout.tie(nullptr);
  30. freopen("height.in", "r", stdin);
  31. freopen("height.out", "w", stdout);
  32. cin >> N;
  33. if (N == 0){
  34. cout << 0;
  35. return 0;
  36. }
  37. Node tree[N];
  38. for (int i = 0; i < N; i++){
  39. cin >> tree[i].K >> tree[i].L >> tree[i].R;
  40. }
  41. cout << height(tree,0)+1;
  42. return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement