From 3235335a6065805c5860dd4c72fbcf816ceac376 Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Fri, 17 Apr 2020 23:33:28 +0530 Subject: [PATCH] Modified with typo errors --- dynamic_programming/tree_height.cpp | 35 ++++++++++++----------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/dynamic_programming/tree_height.cpp b/dynamic_programming/tree_height.cpp index 2ef9f6b6b..fb3025039 100644 --- a/dynamic_programming/tree_height.cpp +++ b/dynamic_programming/tree_height.cpp @@ -1,22 +1,24 @@ -/// C++ Program to find height of the tree using bottom0-up DP. +/// C++ Program to find height of the tree using bottom - up DP. /** * Given a rooted tree with node 1. * Task is to find the height of the tree. + * Example: - + * 4 + * 1 2 + * 1 3 + * 2 4 + * Height of the tree : - 3 **/ #include #include - /// global declarations - /// no of nodes max limit. const int MAX = 1e5; - /// adjacency list std::vector adj[MAX]; - std::vector visited; std::vector dp; @@ -31,37 +33,28 @@ void dp_with_dfs(int u) { child_height = std::max(child_height, dp[v]+1); } } - /// assigned the max child height to current visited node. dp[u] = child_height; } -int main(){ - +int main() { /// number of nodes int n; std::cin >> n; - - int u,v; - - /// a valid tree contains exactly n-1 edges where n denotes the nodes. + int u, v; + /// Tree contains exactly n-1 edges where n denotes the nodes. for (int i=0; i < n-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(n+1, false); /// initialize depth of all nodes to 0. - dp.assign(n+1,0); - - /// call to dp_with_dfs which will initialize the height of all nodes in dp vector. + dp.assign(n+1, 0); + /// function call which will initialize the height of all nodes. dp_with_dfs (1); - - std::cout << dp[1] << std::endl; + std::cout << "Height of the Tree : " << dp[1] << std::endl; }