 ### C++ program to find sum of all the parent nodes having child node x

/**C++ program to find sum of all the parent nodes having child node x**/
/**Calculating sum of all root node having a particuler child node data
can be done using recursive function. In this programme we need to
check for every node have a particuler child node data value or not.
If any child node have that data value then we need to add root node
data with previous existing sum.
10
/     \
11       9
/    \    /   \
7   12 11   8
As in this programme we need to check for each node so that time
complexity for that programme will be O(n) where n is the total no
of node of the binary tree.**/
#include<iostream>
#include<queue>
using namespace std;
struct Node
{
int data;
struct Node* left, *right;
};
struct Node* newNode(int value)
{
struct Node* temp =new Node;
temp->data = value;
temp->left = temp->right = NULL;
return temp;
};
/**insertion of new node in a binary tree**/
void insert(struct Node* temp, int value)
{
queue<struct Node*> q;
q.push(temp);
while(!q.empty())
{
struct Node* temp = q.front();
q.pop();
if(!temp->left)
{
temp->left = newNode(value);
break;
}
else
q.push(temp->left);
if (!temp->right)
{
temp->right = newNode(value);
break;
}
else
q.push(temp->right);
}
}
/**calculate sum of value of all root node whose any of the child having given value**/
int sum(struct Node* root,int value,int s)
{
struct Node *temp=root;
if(temp->left==nullptr || temp->right==nullptr)
return s;
else if(temp->left->data==value || temp->right->data==value)
s=s+temp->data;
s=sum(temp->left,value,s);
s=sum(temp->right,value,s);
return s;
}
int main()
{
int value,t,s;
struct Node* root = newNode(10);                    //insertion of element;
root->left = newNode(11);
root->left->left = newNode(7);
root->right = newNode(9);
root->right->left = newNode(11);
root->right->right = newNode(8);
t = 12;
insert(root,t);
cout << "Enter the number:" << endl;                //Enter the searching element;
cin>>value;
cout << "Sum of all root node whose child have given value:\n";
s=sum(root,value,0);
cout << s;
return 0;
}
/**
Input:
Enter the number:
11
Output:
Sum of all root node whose child have given value:
19
**/ #### More Articles of Ezaz Hossain Sardar:

Name Views Likes
Python Program to Find the first Common Element between the 2 given Linked Lists 254 20
C++ std::valarray_function_name 320 23
C++ std::typeindex_function_name 220 14
C++ std::condition_variable 299 16
C++ mutex mutex_function_name 299 24
C++ std:: 234 20
C++ std::is_trivially_default_constructible 295 20
C++ std::is_trivially_copy_constructible 302 29
C++ std::is_signed 294 25
C++ std::is_scalar 246 15
C++ std::is_same 324 22
C++ std::is_rvalue_reference 269 15
C++ std::is_reference 212 17
C++ std::is_object 257 15
C++ std::is_nothrow_destructible 334 26
C++ std::is_nothrow_constructible 300 22
C++ std::is_member_pointer 240 16
C++ std::is_member_function_pointer 263 19
C++ std::is_literal_type 305 26
C++ std::is_fundamental 262 16
C++ std::is_compound 267 20
C++ std::move for list 298 29
C++ std::move for array 287 22
C++ std::move 274 20
C++ std::replace for array 853 17
C++ std::replace 296 20
C++ std::replace_if 296 22
C++ std::minmax 325 27
C++ std::minmax_element 299 11
C++ std::remove 241 12
C++ std::remove_if 266 18
C++ std::replace_copy 254 20
C++ std::replace_copy_if 234 11
C++ std::remove_copy 272 19
C++ program to find get level of a node in binary tree using recursion 274 28
C++ program to find size of a tree without using recursion 202 14
C++ program to find height of a tree without using recursion 212 17
C++ program to find diameter of a binary tree in O(n) 270 23
C++ program to find diameter of a binary tree 245 13
C++ program to find height of a tree using recursion 215 17
C++ program to count non-leaf nodes in a binary tree using recursion 320 19
C++ program to count leaf nodes in a binary tree using recursion 672 13
C++ program to find sum of all leaf nodes of binary tree 251 21
C++ program to find sum of all the parent nodes having child node x 403 23
C++ program to print path from root to a given node in a binary tree 245 16
C++ program to find distance from root to given node in a binary tree 264 18