forked from SnowScriptWinterOfCode/LeetCode_Q
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request SnowScriptWinterOfCode#529 from aishwarya-chandra/…
…day22q2 day22q2
- Loading branch information
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
``` |