Given a Binary Tree, find the maximum height difference in the tree i.e. |height of Left subtree – height of right subtree|. code example
Example 1: height of a binary tree
int height(Node* root)
{
if (root == nullptr)
return 0;
return 1 + max(height(root->left), height(root->right));
}
Example 2: find height of a tree
int maxDepth(node* node)
{
if (node == NULL)
return 0;
else
{
int lDepth = maxDepth(node->left);
int rDepth = maxDepth(node->right);
if (lDepth > rDepth)
return(lDepth + 1);
else return(rDepth + 1);
}
}