Size of Binary search tree

 The size of BST is:

(size of left subtree)+1(root)+(size of right subtree)

(size of left sub-subtree)+1(left-sub-root)+(size of right sub-tree) + 1(root) + (size of left sub-subtree)+1(right-sub-root)+(size of right sub-tree)

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


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