Skip to content

Commit 200a2dd

Browse files
committed
Time: 0 ms (100%), Space: 43.5 MB (6.71%) - LeetHub
1 parent 8f2acb8 commit 200a2dd

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

LeetCode/Easy/0404-sum-of-left-leaves/0404-sum-of-left-leaves.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
*/
1616
class Solution {
1717
Queue<TreeNode> queue = new ArrayDeque<>();
18+
int total = 0;
1819

1920
public int sumOfLeftLeaves(TreeNode root) {
20-
int total = 0;
2121

2222
// //bfs
2323
// queue.add(root);
@@ -33,13 +33,22 @@ public int sumOfLeftLeaves(TreeNode root) {
3333
// return total;
3434

3535
//dfs
36-
if(root == null) return 0;
36+
// if(root == null) return 0;
3737

38-
if(root.left!=null){
39-
if(root.left.left==null && root.left.right==null) total+=root.left.val;
40-
}
38+
// if(root.left!=null){
39+
// if(root.left.left==null && root.left.right==null) total+=root.left.val;
40+
// }
41+
42+
// return total + sumOfLeftLeaves(root.left)+sumOfLeftLeaves(root.right);
43+
dfs(root);
44+
return total;
45+
}
46+
47+
void dfs(TreeNode node){
48+
if(node == null) return;
49+
if(node.left!=null && node.left.left==null&&node.left.right==null) total+=node.left.val;
4150

42-
return total + sumOfLeftLeaves(root.left)+sumOfLeftLeaves(root.right);
43-
// if(root.right!=null) sumOfLeftLeaves(root.right);
51+
dfs(node.left);
52+
dfs(node.right);
4453
}
4554
}

0 commit comments

Comments
 (0)