Height of Binary Tree

1) If node is null then return 0.
2) Else we call the recursive function, height for left and right subtree and choose their maximum.
3) We also add 1 to the result which indicates height of root of the tree.

int height(struct Node* root)
    {
        if(root==0)
        return 0;
        else
       
return 1+max(height(root->right),height(root->left));
    }

Comments

Popular posts from this blog

First_Come_First_Serve CPU Scheduling

Reversing stack Method 2 !! (One Helper Stack only)

Populating Next Right Pointers in Each Node in O(1) space (without queue and level order)

Calculate factorial of large numbers !! (Using Arrays)

Multiplication of large numbers (Given in string format)

Left View of Binary Tree (Method 1 using recursion)

Check Bracket Sequence

Image Multiplication

Boundary Traversal of binary tree

BST to greater sum tree