Skip to content

Commit

Permalink
Merge pull request SnowScriptWinterOfCode#529 from aishwarya-chandra/…
Browse files Browse the repository at this point in the history
…day22q2

day22q2
  • Loading branch information
bh-g authored Jan 23, 2024
2 parents 68abfd4 + 7732470 commit 5fb5870
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Day-22/q2: Sum of Left Leaves/aishwarya--J.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
```
/**
* DFS Recursive
*
* Time Complexity: O(N). All nodes will be visited.
*
* Space Complexity: O(H). Stack space.
* In case of balanced tree (best case) it will be O(log N) and in case of Skewed Tree (worst case) it will be O(N)
*
* N = Number of nodes. H = Height of the tree.
*/
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) {
return 0;
}
// Checking if left Node is a leaf node
if (root.left != null && root.left.left == null && root.left.right == null) {
return root.left.val + sumOfLeftLeaves(root.right);
}
// Exploring the tree further.
return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);
}
}
```

0 comments on commit 5fb5870

Please sign in to comment.