Skip to content

Commit 207a95b

Browse files
committed
commit
1 parent 632abd8 commit 207a95b

File tree

6 files changed

+228
-133
lines changed

6 files changed

+228
-133
lines changed

.idea/encodings.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 124 additions & 133 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

[0404][Sum of Left Leaves]/[0404][Sum of Left Leaves].iml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,15 @@
77
</content>
88
<orderEntry type="inheritedJdk" />
99
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="module-library">
11+
<library name="JUnit4">
12+
<CLASSES>
13+
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
14+
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
15+
</CLASSES>
16+
<JAVADOC />
17+
<SOURCES />
18+
</library>
19+
</orderEntry>
1020
</component>
1121
</module>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* @author: wangjunchao(王俊超)
6+
* @time: 2019-06-30 14:18
7+
**/
8+
public class Main {
9+
@Test
10+
public void test1() {
11+
TreeNode root = new TreeNode(3);
12+
root.left =new TreeNode(9);
13+
root.right = new TreeNode(20);
14+
root.right.left = new TreeNode(15);
15+
root.right.right = new TreeNode(7);
16+
17+
Solution solution = new Solution();
18+
Assert.assertEquals(24, solution.sumOfLeftLeaves(root));
19+
}
20+
21+
@Test
22+
public void test2() {
23+
TreeNode root = new TreeNode(1);
24+
25+
Solution solution = new Solution();
26+
Assert.assertEquals(0, solution.sumOfLeftLeaves(root));
27+
}
28+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
/**
3+
* @author: wangjunchao(王俊超)
4+
* @time: 2019-06-30 14:11
5+
**/
6+
public class Solution {
7+
/**
8+
* <pre>
9+
* Find the sum of all left leaves in a given binary tree.
10+
*
11+
* Example:
12+
*
13+
* 3
14+
* / \
15+
* 9 20
16+
* / \
17+
* 15 7
18+
*
19+
* There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
20+
* </pre>
21+
*
22+
* @param root
23+
* @return
24+
*/
25+
public int sumOfLeftLeaves(TreeNode root) {
26+
int[] result = new int[1];
27+
sumOfLeftLeaves(root, false, result);
28+
return result[0];
29+
}
30+
31+
/**
32+
* 计算所有左叶子的和
33+
* @param root
34+
* @param result
35+
*/
36+
private void sumOfLeftLeaves(TreeNode root, boolean isLeftLeaf, int[] result) {
37+
38+
if (root == null) {
39+
return;
40+
}
41+
42+
if (root.left == null && root.right==null && isLeftLeaf) {
43+
result[0] += root.val;
44+
return;
45+
}
46+
47+
sumOfLeftLeaves(root.left, true, result);
48+
sumOfLeftLeaves(root.right, false, result);
49+
}
50+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Author: Íõ¿¡³¬
3+
* Date: 2015-08-19
4+
* Time: 06:45
5+
* Declaration: All Rights Reserved !!!
6+
*/
7+
public class TreeNode {
8+
int val;
9+
TreeNode left;
10+
TreeNode right;
11+
12+
TreeNode(int x) {
13+
val = x;
14+
}
15+
}

0 commit comments

Comments
 (0)