1int height(Node* root)
2{
3 // Base case: empty tree has height 0
4 if (root == nullptr)
5 return 0;
6
7 // recur for left and right subtree and consider maximum depth
8 return 1 + max(height(root->left), height(root->right));
9}
10
1// finding height of a binary tree in c++.
2int maxDepth(node* node)
3{
4 if (node == NULL)
5 return 0;
6 else
7 {
8 /* compute the depth of each subtree */
9 int lDepth = maxDepth(node->left);
10 int rDepth = maxDepth(node->right);
11
12 /* use the larger one */
13 if (lDepth > rDepth)
14 return(lDepth + 1);
15 else return(rDepth + 1);
16 }
17}