Write the recursive code to find the height of a binary tree.

CS 201 DATA STRUCTURES
QUIZ  SOLUTION

PROBLEM
Write the recursive code to find the height of a binary tree.
SOLUTION

template <class type>
int BST<type>::Height()
{
            return Height(root);
}


template <class type>
int BST<type>::Height(node<type> *n)
{
            if (!n)
                        return 0;
            int h1=0, h2=0;
            h1 = Height(n->left);
            h2 = Height(n->right);
           
            return max(h1,h2);                   //implement max function yourself
}


Comments

Popular Posts