-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumOfLeftLeaves.c
More file actions
48 lines (46 loc) · 867 Bytes
/
SumOfLeftLeaves.c
File metadata and controls
48 lines (46 loc) · 867 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int SearchLeaves(struct TreeNode* node, int NodeType)
{
struct TreeNode* right_child = node->right;
struct TreeNode* left_child = node->left;
int left_sum = 0, right_sum = 0;
if(left_child == NULL && right_child == NULL)
{
if(NodeType == 0)
{
return node->val;
}
else
{
return 0;
}
}
if(left_child != NULL)
{
left_sum = SearchLeaves(left_child,0);
}
if(right_child != NULL)
{
right_sum = SearchLeaves(right_child,1);
}
return left_sum + right_sum;
}
int sumOfLeftLeaves(struct TreeNode* root)
{
if(root == NULL)
{
return 0;
}
if(root->left == NULL && root->right == NULL)
{
return 0;
}
return SearchLeaves(root, 0);
}