Answers for "compute tree height"

2

height of the tree

int height(Node* root) {
        // Base Condition : if root is already null. then height must be -1 to make balance with recursion call...
        if(!root) return 0;
        
        // Actual Return statement.. for recursion call..
        return 1 + max(height(root->left), height(root->right));
    }
Posted by: Guest on December-02-2021
8

find height of a tree

// finding height of a binary tree in c++.
int maxDepth(node* node)  
{  
    if (node == NULL)  
        return 0;  
    else
    {  
        /* compute the depth of each subtree */
        int lDepth = maxDepth(node->left);  
        int rDepth = maxDepth(node->right);  
      
        /* use the larger one */
        if (lDepth > rDepth)  
            return(lDepth + 1);  
        else return(rDepth + 1);  
    }  
}
Posted by: Guest on June-05-2020
0

compute tree height

recursive tree height
Posted by: Guest on March-27-2022

Python Answers by Framework

Browse Popular Code Answers by Language