leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1315.cpp (860B)
0 class Solution {
1 public:
2 int sumEvenGrandparent(TreeNode *root) {
3 stack<TreeNode *> st;
4 int sum = 0;
6 st.push(root);
7 while (!st.empty()) {
8 TreeNode *root = st.top();
9 st.pop();
11 if (root->left) {
12 st.push(root->left);
13 if (root->val % 2 == 0) {
14 if (root->left->left) sum += root->left->left->val;
15 if (root->left->right) sum += root->left->right->val;
16 }
17 }
19 if (root->right) {
20 st.push(root->right);
21 if (root->val % 2 == 0) {
22 if (root->right->left) sum += root->right->left->val;
23 if (root->right->right) sum += root->right->right->val;
24 }
25 }
26 }
28 return sum;
29 }
30 };