/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int all = 0;
int sumEvenGrandparent(TreeNode* root) {
TreeNode* p = root;
Judgenode(p);
return all;
}
void Judgenode(TreeNode* p){//判断p的左右孩子
int num = p->val;
if(p->left){
Judge(p->left,num);//传入p的值和p的左孩子
Judgenode(p->left);
}
if(p->right){
Judge(p->right,num);//传入p的值和p的右孩子
Judgenode(p->right);
}
}
void Judge(TreeNode* p,int num){//传进来的是p的左或右孩子
if(num%2 != 0){//判断祖父节点值是否为偶数
return ;
}
if(p->left){//判断p的孙子结点
all += p->left->val;
}
if(p->right){//判断p的孙子结点
all += p->right->val;
}
return ;
}
};