Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. #include <fstream>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. using namespace std;
  5.  
  6. int tree[200001][3];
  7. int hgt = -1;
  8.  
  9. void height(int curr, int h){
  10.     hgt = (h > hgt) ? h : hgt;
  11.     if (tree[curr][1] != 0){
  12.         height(tree[curr][1], h+1);
  13.     }
  14.     if (tree[curr][2] != 0){
  15.         height(tree[curr][2], h+1);
  16.     }
  17.  
  18. }
  19. int main() {
  20.     ifstream fin("height.in");
  21.     ofstream fout("height.out");
  22.  
  23.     int n, i, temp;
  24.     fin >> n;
  25.     if (n == 0){
  26.         fout << 0;
  27.         return 0;
  28.     }
  29.  
  30.     for (i = 1; i <= n; i++) {
  31.         fin >> tree[i][0];
  32.         fin >> tree[i][1];
  33.         fin >> tree[i][2];
  34.     }
  35.  
  36.     height(1, 1);
  37.  
  38.     fout << hgt;
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement