I am trying to find the height of a Binary tree with this code, but it keeps returning 0, can someone tell me why?

D3v

I am trying to find the height of a Binary tree with this code, but it keeps returning 0, can someone please tell me why?

int heightHelper(Node* root, int maxheight, int rootheight)
{

    if (root->right == nullptr && root->left == nullptr) { //checks if the node has a child
        if (maxheight < rootheight) {
            maxheight = rootheight;
        }
    }
    else { //if it has then increase height by 1
        rootheight += 1;

        if (root->left != nullptr) {
            heightHelper(root->left, maxheight, rootheight);
        }
        if (root->right != nullptr) {
            heightHelper(root->right, maxheight, rootheight);
        }
    }

    return maxheight; //return height
}

int height(Node* root)
{
    // Write your code here.

    return heightHelper(root, 0, 0); //root node base case
}
Maxime Franchot

You're mixing up two ways of doing things. Either pass the result parameter, using a pointer or an address (int* and &maxheight). In this case there is no return value. But tbh, this is a coding style I've mainly only seen in hardware programming.

Otherwise you can return a result. You already have a return parameter in place, just fix the actual usage of the value because your code ignores it.

Here's the two ways to do it:

Using return parameter:

int heightHelper(Node* root, int height)
{

    if (root->right == nullptr && root->left == nullptr) { //checks if the node has a child
        return height;
    }
    else { //if it has then increase height by 1
        height += 1;
        int maxheight1;
        int maxheight2;

        if (root->left != nullptr) {
            maxheight1 = heightHelper(root->left, height);
        }
        if (root->right != nullptr) {
            maxheight2 = heightHelper(root->right, height);
        }

        // return maximum of the two
        if (maxheight1 > maxheight2) return maxheight1;
        else return maxheight2;
    }
}

int height(Node* root)
{
    // Write your code here.

    return heightHelper(root, height); //root node base case
}

Using pointer parameters. Note that rootheight is local to each branch so don't pass it as a pointer.

void heightHelper(Node* root, int* maxheight_ptr, int rootheight)
    {

    if (root->right == nullptr && root->left == nullptr) { //checks if the node has a child
        if (*maxheight < rootheight) {
            *maxheight = rootheight;
        }
    }
    else { //if it has then increase height by 1
        rootheight += 1;

        if (root->left != nullptr) {
            heightHelper(root->left, maxheight_ptr, rootheight);
        }
        if (root->right != nullptr) {
            heightHelper(root->right, maxheight_ptr, rootheight);
        }
    }
}

int height(Node* root)
{
    // Write your code here.

    int maxheight = 0;


    heightHelper(root, &maxheight, 0); //pass the addresses of variables

    return maxheight;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Can someone please tell me where am I wrong in this python code?

Can someone tell me the error in my code?

Can someone tell me why this is not a constant expression?

Can someone explain to me this recursive code for binary tree?

Can someone please tell me why my apps crashes when I press the cam button

My code keeps returning 'none' as a value, I am not sure why

Can someone please tell me whats wrong with this code

Can someone please tell me why I'm not understanding scope in this example?

Can someone tell me the mistake in this sql code?

I don't see why this code is not working! can someone please tell me what i am doing wrong?

Can someone please help me with the code I am writing? (Java)

I am trying to add at the back of the linked list but i keep getting segmentation error, can someone explain why?

Python, I am trying to make a leader board but it wont save in the text file can someone help me?

Can somebody tell me why my "thumbnails" aren't in the row format I am attempting in code link?

Can someone tell me why one of these JavaScript code works and one doesn't?

Can someone tell me why this program keeps crashing?

Can someone tell me if Andorid studio 3.0.1 has component tree view and if there is where can i enable it from?

I am trying to wrap my head around below elasticsearch dsl. Can someone tell me how `must` clause is used below

Can anyone tell me why this code doesn't work and keeps printing 0.00 km as the shortest distance?

Can someone check my Dart code and tell me where I'm making mistake in returning data from my screen as a ListView

Can anybody tell me why my code is returning my Z array to be 0? (CUDA C)

I don't know why I get this 'literal does not match format string', can someone tell me?

I am trying to scrape multiple pages using beautfiul soup but the code keeps returning the same data for every page

can someone tell me why my js code is not printing the date using the arrow function?

Can someone tell me why my code doesn`t work with this condition?

Can someone tell me why I am getting a 'null' output after execution?

Can someone tell me why I'm getting these errors and how do I resolve them?

I can't seem to insert a node in a tree. Can someone take a look at my code and tell me where did I do wrong?

Can someone explain to me why i am getting an exception thrown with this code