dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: How to find the height of a binary tree?

  1. #1
    Guest

    How to find the height of a binary tree?

    Can any one please write a program to find the height(number of nodes) of a binary tree. A recurssive program will be helpful.
    Thank You,
    Prasad



  2. #2
    Join Date
    Apr 2010
    Posts
    1

    Re: How to find the height of a binary tree?

    ok:

    In java, to find the number of nodes:

    public int numNodes(TreeNode t){

    if (t == null)
    return 0;
    return (1 + numNodes(t.leftChild) + numNodes(t.rightChild);

    }

    To find the height:

    public int height(TreeNode t){

    if (t == null)
    return 0;
    int heightLeft = height(t.leftChild);
    int heightRight = height(t.rightChild)

    if( heightLeft > heightRight )
    return heightLeft +1;
    else
    return heightRight +1;
    }

    Do not think that the number of nodes is the same as height.

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: How to find the height of a binary tree?

    Please do not resurrect old threads - this question was asked nearly 10 years ago.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)