commit 897a722df2a57e8aaac99f88a8eaf41c712022e8
parent e75c5c8414f0254d98d1b7ba205820085e966067
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date: Sun, 30 Apr 2023 17:40:43 -0500
Completed sum of left leaves using dart
Diffstat:
1 file changed, 38 insertions(+), 0 deletions(-)
diff --git a/sum-of-left-leaves/sum-of-left-leaves.dart b/sum-of-left-leaves/sum-of-left-leaves.dart
@@ -0,0 +1,38 @@
+//Find the sum of all leaves that are left of their parent.
+//to do this I used DFS to recurse through the list and if the current node is
+//a leaf it checks if it is left. If it is a left leaf then the stored value of it is
+//added onto the total integer. The time complexity of this code is O(n) where n is the number
+//of nodes in the tree.
+//Runtime: 266ms Beats: 100%
+//Memory: 142.3MB Beats: 100%
+
+
+/**
+ * Definition for a binary tree node.
+ * class TreeNode {
+ * int val;
+ * TreeNode? left;
+ * TreeNode? right;
+ * TreeNode([this.val = 0, this.left, this.right]);
+ * }
+ */
+class Solution {
+ int total = 0;
+ int sumOfLeftLeaves(TreeNode? root) {
+ recurse(root ?? new TreeNode(), false);
+ return total;
+ }
+
+ void recurse(TreeNode node, bool is_left ){
+ if(node.left != null){
+ recurse(node.left ?? new TreeNode(), true);
+ }
+ if(node.right != null){
+ recurse(node.right ?? new TreeNode(), false);
+ }
+ if(is_left && node.right == null && node.left == null){
+ total += node.val;
+ }
+ }
+
+}