File tree Expand file tree Collapse file tree
LeetCode/Easy/0404-sum-of-left-leaves Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1515 */
1616class 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}
You can’t perform that action at this time.
0 commit comments