Sum of Nodes in BST

 In order to get the sum of nodes of binary tree,  add the value of the root node, and move to the left subtree and right subtree, if root have reached to the end, i.e. after root->left=0, root will store 0, in that case return 0.

long int sumBT(Node* root)
{
    if (root==0)
    return 0;
    else 
    return root->key+sumBT(root->right)+sumBT(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