#include <bits/stdc++.h>
using
namespace
std;
struct
Node {
int
data;
Node *left, *right;
};
Node* newNode(
int
data)
{
Node* temp =
new
Node();
temp->data = data;
temp->left = temp->right = NULL;
}
void
leafSumFunc(Node* root,
int
* leafSum)
{
if
(!root)
return
;
if
(!root->left && !root->right)
*leafSum += root->data;
leafSumFunc(root->left, leafSum);
leafSumFunc(root->right, leafSum);
}
int
sumParentChildDiff(Node* root)
{
if
(!root)
return
0;
if
(!root->left && !root->right)
return
root->data;
int
leafSum = 0;
leafSumFunc(root, &leafSum);
return
(root->data - leafSum);
}
int
main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right = newNode(3);
root->right->right = newNode(7);
root->right->left = newNode(6);
cout << sumParentChildDiff(root);
return
0;
}