Skip to content

Commit 24839ce

Browse files
committed
New Problem Solution "Sum of Left Leaves"
1 parent a991e74 commit 24839ce

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ LeetCode
88

99
| # | Title | Solution | Difficulty |
1010
|---| ----- | -------- | ---------- |
11+
|404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [C++](./algorithms/cpp/removeKDigits/sumOfLeftLeaves/SumOfLeftLeaves.cpp)|Easy|
1112
|402|[Remove K Digits](https://leetcode.com/problems/remove-k-digits/) | [C++](./algorithms/cpp/removeKDigits/RemoveKDigits.cpp)|Medium|
1213
|401|[Binary Watch](https://leetcode.com/problems/binary-watch/) | [C++](./algorithms/cpp/binaryWatch/BinaryWatch.cpp)|Easy|
1314
|400|[Nth Digit](https://leetcode.com/problems/nth-digit/) | [C++](./algorithms/cpp/nthDigit/NthDigit.cpp)|Easy|
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Source : https://leetcode.com/problems/sum-of-left-leaves/
2+
// Author : Hao Chen
3+
// Date : 2016-11-12
4+
5+
/***************************************************************************************
6+
*
7+
* Find the sum of all left leaves in a given binary tree.
8+
*
9+
* Example:
10+
*
11+
* 3
12+
* / \
13+
* 9 20
14+
* / \
15+
* 15 7
16+
*
17+
* There are two left leaves in the binary tree, with values 9 and 15 respectively.
18+
* Return 24.
19+
***************************************************************************************/
20+
21+
/**
22+
* Definition for a binary tree node.
23+
* struct TreeNode {
24+
* int val;
25+
* TreeNode *left;
26+
* TreeNode *right;
27+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
28+
* };
29+
*/
30+
class Solution {
31+
public:
32+
33+
34+
void sumOfLeftLeaves_recursion_v1(TreeNode* root, int& result) {
35+
if (root == NULL ) {
36+
return;
37+
}
38+
39+
if (root->left && root->left->left == NULL && root->left->right == NULL) {
40+
result += root->left->val;
41+
}
42+
sumOfLeftLeaves_recursion_v1(root->left, result);
43+
sumOfLeftLeaves_recursion_v1(root->right, result);
44+
45+
}
46+
47+
int sumOfLeftLeaves_recursion_v2(TreeNode* root) {
48+
if (root == NULL ) {
49+
return 0;
50+
}
51+
int result = 0;
52+
if (root->left && root->left->left == NULL && root->left->right == NULL) {
53+
result = root->left->val;
54+
}
55+
result += sumOfLeftLeaves_recursion_v2(root->left) + sumOfLeftLeaves_recursion_v2(root->right);
56+
return result;
57+
}
58+
59+
60+
int sumOfLeftLeaves(TreeNode* root) {
61+
srand(time(NULL));
62+
if (rand()%2) {
63+
int result = 0;
64+
sumOfLeftLeaves_recursion_v1(root, result);
65+
return result;
66+
} else {
67+
return sumOfLeftLeaves_recursion_v2(root);
68+
}
69+
70+
}
71+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy