Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit24839ce

Browse files
committed
New Problem Solution "Sum of Left Leaves"
1 parenta991e74 commit24839ce

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+
classSolution {
31+
public:
32+
33+
34+
voidsumOfLeftLeaves_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+
intsumOfLeftLeaves_recursion_v2(TreeNode* root) {
48+
if (root ==NULL ) {
49+
return0;
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+
intsumOfLeftLeaves(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+
returnsumOfLeftLeaves_recursion_v2(root);
68+
}
69+
70+
}
71+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp