diff options
Diffstat (limited to '02-data_structures/01-basic_data_structures/02-tree_height/tree-height.cpp')
-rw-r--r-- | 02-data_structures/01-basic_data_structures/02-tree_height/tree-height.cpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/02-data_structures/01-basic_data_structures/02-tree_height/tree-height.cpp b/02-data_structures/01-basic_data_structures/02-tree_height/tree-height.cpp new file mode 100644 index 0000000..d050e88 --- /dev/null +++ b/02-data_structures/01-basic_data_structures/02-tree_height/tree-height.cpp @@ -0,0 +1,35 @@ +#include <iostream> +#include <vector> +#include <algorithm> + +class TreeHeight { + int n; + std::vector<int> parent; + +public: + void read() { + std::cin >> n; + parent.resize(n); + for (int i = 0; i < n; i++) + std::cin >> parent[i]; + } + + int compute_height() { + // Replace this code with a faster implementation + int maxHeight = 0; + for (int vertex = 0; vertex < n; vertex++) { + int height = 0; + for (int i = vertex; i != -1; i = parent[i]) + height++; + maxHeight = std::max(maxHeight, height); + } + return maxHeight; + } +}; + +int main() { + std::ios_base::sync_with_stdio(0); + TreeHeight tree; + tree.read(); + std::cout << tree.compute_height() << std::endl; +} |