<p>Given the <code>root</code> of a binary tree, return <em>the sum of values of nodes with an <strong>even-valued grandparent</strong></em>. If there are no nodes with an <strong>even-valued grandparent</strong>, return <code>0</code>.</p>

<p>A <strong>grandparent</strong> of a node is the parent of its parent if it exists.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/10/even1-tree.jpg" style="width: 504px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]
<strong>Output:</strong> 18
<strong>Explanation:</strong> The red nodes are the nodes with even-value grandparent while the blue nodes are the even-value grandparents.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/10/even2-tree.jpg" style="width: 64px; height: 65px;" />
<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li>
	<li><code>1 &lt;= Node.val &lt;= 100</code></li>
</ul>

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
 * right(right) {}
 * };
 */
void check(TreeNode* ptr, long long &ans);
void findSum(TreeNode* ptr, int count, int& sum);
class Solution {
public:
    int sumEvenGrandparent(TreeNode* root) {
        TreeNode* ptr = root;
        long long ans = 0;
        check(ptr, ans);
        return ans;
    }
};
void check(TreeNode* ptr, long long &ans) {
    if (ptr->val % 2 == 0) {
        int count = 3;
        int sum = 0;
        findSum(ptr, count, sum);
        ans+=sum;
    }
    if (ptr->left) {
        check(ptr->left, ans);
    }
    if (ptr->right) {
        check(ptr->right, ans);
    }
    if (ptr->left && ptr->right) {
        return;
    }
}
void findSum(TreeNode* ptr, int count, int& sum) {
    count--;
    if (!count) {
        cout<<ptr->val<<" ";
        sum += ptr->val;
        return;
    }
    if (ptr->left) {
        findSum(ptr->left, count, sum);
    }
    if (ptr->right) {
        findSum(ptr->right, count, sum);
    }
}
41300