-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1315.cpp
More file actions
31 lines (27 loc) · 860 Bytes
/
Copy path1315.cpp
File metadata and controls
31 lines (27 loc) · 860 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public:
int sumEvenGrandparent(TreeNode *root) {
stack<TreeNode *> st;
int sum = 0;
st.push(root);
while (!st.empty()) {
TreeNode *root = st.top();
st.pop();
if (root->left) {
st.push(root->left);
if (root->val % 2 == 0) {
if (root->left->left) sum += root->left->left->val;
if (root->left->right) sum += root->left->right->val;
}
}
if (root->right) {
st.push(root->right);
if (root->val % 2 == 0) {
if (root->right->left) sum += root->right->left->val;
if (root->right->right) sum += root->right->right->val;
}
}
}
return sum;
}
};