File tree Expand file tree Collapse file tree 2 files changed +45
-0
lines changed
src/main/java/com/brianway/learning/algorithms/leetcode/easy Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -99,6 +99,7 @@ Leetcode problems classified by company:
99
99
| 354| Russian Doll Envelopes| Hard|||
100
100
| 376| Wiggle Subsequence| Medium| Greedy/Dynamic Programming| TODO 一题多解|
101
101
| 392| Is Subsequence| Easy| Dynamic Programming/Two Pointers| 一题多解|
102
+ | 404| Sum of Left Leaves| Easy| Binary Tree||
102
103
| 416| Partition Equal Subset Sum| Medium| Dynamic Programming,01背包||
103
104
| 455| Assign Cookies| Easy| Greedy||
104
105
| 474| Ones and Zeroes| Medium| Dynamic Programming,01背包| TODO 二维解法|
Original file line number Diff line number Diff line change
1
+ package com .brianway .learning .algorithms .leetcode .easy ;
2
+
3
+ import com .brianway .learning .algorithms .leetcode .common .TreeNode ;
4
+
5
+ /**
6
+ * LeetCode 404. Sum of Left Leaves
7
+ * Question https://leetcode.com/problems/sum-of-left-leaves/
8
+ * 关键题设:A leaf is a node with no children.
9
+ * A left leaf is a leaf that is the left child of another node.
10
+ *
11
+ * @auther brian
12
+ * @since 2022/9/6 00:00
13
+ */
14
+ public class SumOfLeftLeaves {
15
+
16
+ public int sumOfLeftLeaves (TreeNode root ) {
17
+ return 0 ;
18
+ }
19
+
20
+ /**
21
+ * 递归
22
+ * 后序遍历
23
+ */
24
+ public class SumOfLeftLeaves0 extends SumOfLeftLeaves {
25
+ @ Override
26
+ public int sumOfLeftLeaves (TreeNode root ) {
27
+ // 终止条件
28
+ if (root == null ) {
29
+ return 0 ;
30
+ }
31
+ // 单层逻辑
32
+ int leftSum = sumOfLeftLeaves (root .left );
33
+ if (root .left != null && root .left .left == null && root .left .right == null ) {
34
+ // 仅这个if条件下才会有val值贡献进来
35
+ leftSum = root .left .val ;
36
+ }
37
+ int rightSum = sumOfLeftLeaves (root .right );
38
+
39
+ return leftSum + rightSum ;
40
+ }
41
+ }
42
+
43
+ }
44
+
You can’t perform that action at this time.
0 commit comments