Examples:
Input : Binary tree with x = 2:
4
/ \
2 5
/ \ / \
7 2 2 3
Output : 11
4
/ \
2 5
/ \ / \
7 2 2 3
The highlighted nodes (4, 2, 5) above
are the nodes having 2 as a child node.
// C++ implementation to find the sum of all
// the parent nodes having child node x
#include <bits/stdc++.h>
using namespace std;
// Node of a binary tree
struct Node
{
int data;
Node *left, *right;
};
// function to get a new node
Node* getNode(int data)
{
// allocate memory for the node
Node *newNode =
(Node*)malloc(sizeof(Node));
// put in the data
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
// function to find the sum of all the
// parent nodes having child node x
void sumOfParentOfX(Node* root, int& sum, int x)
{
// if root == NULL
if (!root)
return;
//当左右两节点有其中一个等于x的时候将父节点的值加到sum
if ((root->left && root->left->data == x) ||
(root->right && root->right->data == x))
sum += root->data;
//遍历递归左子树和右子树,找到下一个符合条件的父节点,加到sum
sumOfParentOfX(root->left, sum, x);
sumOfParentOfX(root->right, sum, x);
}
// utility function to find the sum of all
// the parent nodes having child node x
int sumOfParentOfXUtil(Node* root, int x)
{
int sum = 0;
sumOfParentOfX(root, sum, x);
// required sum of parent nodes
return sum;
}
// Driver program to test above
int main()
{
// binary tree formation
Node *root = getNode(4); /* 4 */
root->left = getNode(2); /* / \ */
root->right = getNode(5); /* 2 5 */
root->left->left = getNode(7); /* / \ / \ */
root->left->right = getNode(2); /* 7 2 2 3 */
root->right->left = getNode(2);
root->right->right = getNode(3);
int x = 2;
cout << "Sum = "
<< sumOfParentOfXUtil(root, x);
return 0;
}