-
Notifications
You must be signed in to change notification settings - Fork 0
/
1315 Sum of Nodes with Even-valued grandparent.c
95 lines (79 loc) · 1.74 KB
/
1315 Sum of Nodes with Even-valued grandparent.c
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
typedef struct
{
struct TreeNode * arr_ptr[10000];
int top;
}Stack;
int isStackFull(Stack * tmp_stack)
{
if(tmp_stack->top == 10000-1)
return 1;
else
return 0;
}
int isStackEmpty(Stack * tmp_stack)
{
if(tmp_stack->top == -1)
return 1;
else
return 0;
}
void push(Stack * tmp_stack, struct TreeNode * element)
{
if(isStackFull(tmp_stack))
return;
tmp_stack->arr_ptr[++(tmp_stack->top)] = element;
}
struct TreeNode * pop(Stack * tmp_stack)
{
if(isStackEmpty(tmp_stack))
return NULL;
return tmp_stack->arr_ptr[(tmp_stack->top)--];
}
int peek(Stack * tmp_stack, int index)
{
return tmp_stack->arr_ptr[index]->val;
}
int ret_sum = 0;
int even_val_gp(struct TreeNode * root, Stack * tmp_stack)
{
if(root == NULL)
return 0;
if(tmp_stack->top < 1)
{
push(tmp_stack, root);
even_val_gp(root->left, tmp_stack);
even_val_gp(root->right, tmp_stack);
pop(tmp_stack);
}
else
{
int gp_val = peek(tmp_stack, tmp_stack->top-1);
if(gp_val%2 == 0)
{
ret_sum += root->val;
}
if((root->left==NULL) && (root->right == NULL))
return 0;
push(tmp_stack, root);
even_val_gp(root->left, tmp_stack);
even_val_gp(root->right, tmp_stack);
pop(tmp_stack);
}
return ret_sum;
}
int sumEvenGrandparent(struct TreeNode* root)
{
ret_sum = 0;
Stack tmp_stack;
tmp_stack.top = -1;
even_val_gp(root, &tmp_stack);
return ret_sum;
}