-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path404.sum-of-left-leaves.cpp
58 lines (57 loc) · 1.23 KB
/
404.sum-of-left-leaves.cpp
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
/*
* @lc app=leetcode id=404 lang=cpp
*
* [404] Sum of Left Leaves
*
* https://leetcode.com/problems/sum-of-left-leaves/description/
*
* algorithms
* Easy (48.26%)
* Total Accepted: 108.5K
* Total Submissions: 224.7K
* Testcase Example: '[3,9,20,null,null,15,7]'
*
* Find the sum of all left leaves in a given binary tree.
*
* Example:
*
* 3
* / \
* 9 20
* / \
* 15 7
*
* There are two left leaves in the binary tree, with values 9 and 15
* respectively. Return 24.
*
*
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int summ;
void callme(TreeNode* root, bool status){
if(root == NULL) return;
if(root->left == NULL && root->right == NULL && status){
summ += root->val;
return;
}
callme(root->left, true);
callme(root->right, false);
}
int sumOfLeftLeaves(TreeNode* root) {
summ=0;
if(root == NULL) return 0;
callme(root->left, true);
callme(root->right, false);
return summ;
}
};