I am writing the code to calculate minimum depth of binary tree
My solution works well for tree BST if the inputs are 6,4,9,3,5,7,12,2,8
because minimum depth is coming as 3 which is correct.
But when the tree is 3,2 , minimum depth is coming as 1 instead of 2.
My code snippet is:
int minimumHeightRec(TreeNode root)
{
if(root == null) return 0;
int left = minimumHeightRec(root.left);
int right = minimumHeightRec(root.right);
if(left > right) return right + 1;
else return left + 1;
}