sum of all leaf nodes of binary tree
#include<iostream>
using namespace std;
struct node
{
int data;
node* left;
node* right;
};
node* getnode(int value)
{
node *temp=new node;
temp->data=value;
temp->left=NULL;
temp->right=NULL;
return temp;
}
node* insert_bst(node *roots,int value)
{
if(roots==NULL)
{
return getnode(value);
}
if(roots->data>value)
{
roots->left=insert_bst(roots->left,value);
}
else if(roots->data<value)
{
roots->right=insert_bst(roots->right,value);
}
return roots;
}
int leaf_sum(node *roots)
{
if(roots==NULL)
{
return 0;
}
if(roots->left==NULL&&roots->right==NULL)
{
return roots->data;
}
return (leaf_sum(roots->left)+leaf_sum(roots->right));
}
int main()
{
node * root=new node;
root=NULL;
int value;
do
{
cin>>value;
if(value>0)
{
root=insert_bst(root,value);
}
}while(value>0);
//Inorder(root);
cout<<"Sum of all leaf nodes are "<<leaf_sum(root);
return 0;
}