MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / height

Function height

CPP/Trees/Diameter_of_tree.cpp:12–27  ·  view source on GitHub ↗

Function to find height of a tree */

Source from the content-addressed store, hash-verified

10
11/* Function to find height of a tree */
12int height(Node *root, int &ans)
13{
14 if (root == NULL)
15 return 0;
16
17 int left_height = height(root->left, ans);
18
19 int right_height = height(root->right, ans);
20
21 // update the answer, because diameter of a
22 // tree is nothing but maximum value of
23 // (left_height + right_height + 1) for each node
24 ans = max(ans, 1 + left_height + right_height);
25
26 return 1 + max(left_height, right_height);
27}
28
29/* Computes the diameter of binary tree with given root. */
30int diameter(Node *root)

Callers 1

diameterFunction · 0.70

Calls 1

maxFunction · 0.50

Tested by

no test coverage detected