-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
SumOfLeftLeaves.cpp
71 lines (61 loc) · 1.83 KB
/
SumOfLeftLeaves.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
59
60
61
62
63
64
65
66
67
68
69
70
71
// Source : https://leetcode.com/problems/sum-of-left-leaves/
// Author : Hao Chen
// Date : 2016-11-12
/***************************************************************************************
*
* 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:
void sumOfLeftLeaves_recursion_v1(TreeNode* root, int& result) {
if (root == NULL ) {
return;
}
if (root->left && root->left->left == NULL && root->left->right == NULL) {
result += root->left->val;
}
sumOfLeftLeaves_recursion_v1(root->left, result);
sumOfLeftLeaves_recursion_v1(root->right, result);
}
int sumOfLeftLeaves_recursion_v2(TreeNode* root) {
if (root == NULL ) {
return 0;
}
int result = 0;
if (root->left && root->left->left == NULL && root->left->right == NULL) {
result = root->left->val;
}
result += sumOfLeftLeaves_recursion_v2(root->left) + sumOfLeftLeaves_recursion_v2(root->right);
return result;
}
int sumOfLeftLeaves(TreeNode* root) {
srand(time(NULL));
if (rand()%2) {
int result = 0;
sumOfLeftLeaves_recursion_v1(root, result);
return result;
} else {
return sumOfLeftLeaves_recursion_v2(root);
}
}
};