Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8529627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:04:30+00:00 2026-06-11T09:04:30+00:00

I have written code for finding the diameter of binary tree. But I am

  • 0

I have written code for finding the diameter of binary tree. But I am unable to figure out where is it going wrong . The two functions that I have written and their definition are as follows :-

    int btree::diameteroftree(node* leaf)
    { 
     if (leaf==NULL) 
     return 0;

     int lheight = hieghtoftree(leaf->left);
     int rheight = hieghtoftree(leaf->right);

     int ldiameter = diameteroftree(leaf->left);
     int rdiameter = diameteroftree(leaf->right);

     return max(lheight + rheight + 1,max(ldiameter,rdiameter));
   }


   int btree::hieghtoftree(node* leaf)
   {
    int left=0,right=0;
    if(leaf==NULL)
    return -1;
    else
    {
     left=hieghtoftree(leaf->left);
     right=hieghtoftree(leaf->right);
     if(left > right)
     return left +1;
     else
     return right+1;   
    }
   }

I am unable to figure out where am I going wrong here . Can someone let me know …

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:04:31+00:00Added an answer on June 11, 2026 at 9:04 am

    You want to return the number of nodes on the longest path. Therefore, the problem in your algorithm is this line:

    return max(lheight + rheight + 1,max(ldiameter,rdiameter));
    

    where

    rootDiameter = lheight + rheight + 1
    

    is the length of the path from the deepest node of the left tree to the deepest node of the right tree. However, this calculation is not correct. A single node returns a height of 0, so it will not be counted. You have two options:

    1. Change hieghtoftree to return the number of nodes on the deepest path and not the number of “hops”
    2. Address this problem in your summation

    .

    return max(lheight + rheight + 3,max(ldiameter,rdiameter));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a code for finding diameter of Binary Tree. Need suggestions for
Here I have written code for finding median of two sorted arrays: #include<iostream> using
I have written a code for finding least common ancestor of nodes in binary
I have written a code for finding intersection of two multimap which will compare
I have written some code for displaying a drop down list, but the code
I have written code that opens 16 figures at once. Currently, they all open
I have written code in Java to access web cam,and to save image... I
I have written code to perform a function that could take a while to
Hi I have written code like this @Id @Column(nullable=false) @GeneratedValue(strategy=GenerationType.AUTO) public int getUserID() {
I have written this code inside a servlet to delete certain records from three

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.