<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>

class Solution {
public:
    int sumEvenGrandparent(TreeNode* root) {

        int sum  = 0 ;

        vector<TreeNode*> v; 

        helper(root,v) ;

        cout<<v.size()<<endl ;

        for(int i = 0 ; i<v.size() ; i++){
            helper2(v[i],sum);
        }

        return sum ;
        
    }

private  :

      void helper2(TreeNode* root,int& sum){
        
        if(root==NULL){
            return ;
        }

        if(root->left){
            if(root->left->left){
                sum += root->left->left->val;
            }
            if(root->left->right){
                sum += root->left->right->val;
            }
        }

        if(root->right){
            if(root->right->left){
                sum += root->right->left->val;
            }
            if(root->right->right){
                sum += root->right->right->val;
            }
        }
      }

      void helper(TreeNode*root,vector<TreeNode*>& v){
        
        if(root==NULL){
            return ;
        }

        if(root && root->val%2==0){

           int  d = depth(root) ;
            
           if(d-1 >= 2){
              v.push_back(root) ;
           } 

        }

        helper(root->left,v) ;
        helper(root->right,v) ;


    }

   int depth(TreeNode*root){
    if(root==NULL){return 0 ;}
    
     int lh = depth(root->left) ;
     int rh  = depth(root->right) ;

     return 1 + max(lh,rh) ;
   }
};
41800