Sunday, November 8, 2015

Height of a Binary Tree

This program uses the Binary Search Tree program available here: link
The same program can also be used for Binary Tree 

    public int FindHeight()    {
        return FindHeight(this);
    }
   
    private int FindHeight(BinarySearchTree T)    {
        if(T!=null)    {
            return max(FindHeight(T.left), FindHeight(T.right))+1;
        }    else return -1;
    }
   
    private int max(int a, int b)    {
        if (a>b) return a;
        else return b;
    }

No comments:

Post a Comment

UA-39217154-2