개발 로그/알고리즘
Leetcode) 1315. Sum of Nodes with Even-Valued Grandparent c++
k._.
2021. 9. 21. 15:48
/**
* 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) {}
* };
*/
class Solution {
public:
int sumEvenGrandparent(TreeNode* root) {
if(root==nullptr)
return 0;
int sum =0;
if(root->val%2==0){
if(root->left!=nullptr && root->left->left!=nullptr)
sum+=root->left->left->val;
if(root->left!=nullptr && root->left->right!=nullptr)
sum+=root->left->right->val;
if(root->right!=nullptr && root->right->right!=nullptr)
sum+=root->right->right->val;
if(root->right!=nullptr && root->right->left!=nullptr)
sum+=root->right->left->val;
}
return sum + sumEvenGrandparent(root->left) + sumEvenGrandparent(root->right);
}
};