From 56fe935ed5752b132e949fa70fc5237b405584b8 Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Tue, 23 Jan 2024 18:20:45 +0530 Subject: [PATCH 1/2] day22-q2 solution added in python, java --- .../Tech-neophyte--pj.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Day 22/Day 22 q2 Sum of Left Leaves/Tech-neophyte--pj.md diff --git a/Day 22/Day 22 q2 Sum of Left Leaves/Tech-neophyte--pj.md b/Day 22/Day 22 q2 Sum of Left Leaves/Tech-neophyte--pj.md new file mode 100644 index 00000000..404cc98b --- /dev/null +++ b/Day 22/Day 22 q2 Sum of Left Leaves/Tech-neophyte--pj.md @@ -0,0 +1,38 @@ +## Python code: Iterative DFS using Stack +``` +class Solution(object): + def sumOfLeftLeaves(self, root): + """ + :type root: TreeNode + :rtype: int + """ + if not root: + return 0 + stack = [root] + res = 0 + while stack: + u = stack.pop() + if u.left: + stack.append(u.left) + if not u.left.left and not u.left.right: + res += u.left.val + if u.right: + stack.append(u.right) + return res +``` +## Java code: +``` +class Solution { + public int sumOfLeftLeaves(TreeNode root) { + if (root == null) { + return 0; + } + + int sum = 0; + if (root.left != null && root.left.left == null && root.left.right == null) { + sum += root.left.val; + } + return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right) + sum; + } +} +``` From 6e9eefe25c012e14098b423993ded1e6c2463b92 Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Wed, 24 Jan 2024 11:42:49 +0530 Subject: [PATCH 2/2] moved file to Day-22/q2: Sum of Left Leaves --- .../q2: Sum of Left Leaves}/Tech-neophyte--pj.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {Day 22/Day 22 q2 Sum of Left Leaves => Day-22/q2: Sum of Left Leaves}/Tech-neophyte--pj.md (100%) diff --git a/Day 22/Day 22 q2 Sum of Left Leaves/Tech-neophyte--pj.md b/Day-22/q2: Sum of Left Leaves/Tech-neophyte--pj.md similarity index 100% rename from Day 22/Day 22 q2 Sum of Left Leaves/Tech-neophyte--pj.md rename to Day-22/q2: Sum of Left Leaves/Tech-neophyte--pj.md