diff --git a/dynamic_programming/tree_height.cpp b/dynamic_programming/tree_height.cpp index 8c5242ccd..e9039c5a1 100644 --- a/dynamic_programming/tree_height.cpp +++ b/dynamic_programming/tree_height.cpp @@ -27,7 +27,7 @@ void depth_first_search(int u) { int child_height = 1; for (int v : adj[u]) { if (!visited[v]) { - dp_with_dfs(v); + depth_first_search(v); // select maximum sub-tree height from all children. child_height = std::max(child_height, dp[v]+1); @@ -39,21 +39,24 @@ void depth_first_search(int u) { int main() { // number of nodes - int n; - std::cin >> n; + int no_of_nodes; + std::cout << "Enter number of nodes of the tree : " << std::endl; + std::cin >> no_of_nodes; + // u,v denotes an undirected edge of tree. int u, v; // Tree contains exactly n-1 edges where n denotes the nodes. - for (int i = 0; i < n-1; i++) { + std::cout << "Enter edges of the tree : " << std::endl; + for (int i = 0; i < no_of_nodes-1; i++) { std::cin >> u >> v; // undirected tree u -> v and v -> u. adj[u].push_back(v); adj[v].push_back(u); } // initialize all nodes as unvisited. - visited.assign(n+1, false); + visited.assign(no_of_nodes+1, false); // initialize depth of all nodes to 0. - dp.assign(n+1, 0); + dp.assign(no_of_nodes+1, 0); // function call which will initialize the height of all nodes. - dp_with_dfs(1); + depth_first_search(1); std::cout << "Height of the Tree : " << dp[1] << std::endl; }