From f5da57fea2b27c7ffbe3cb67c1a3bdfd507c8855 Mon Sep 17 00:00:00 2001 From: Megha Raykar Date: Fri, 6 Mar 2026 14:04:24 -0800 Subject: [PATCH 1/2] Added Problem1 --- Problem1.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Problem1.py diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..b5c2aa5c --- /dev/null +++ b/Problem1.py @@ -0,0 +1,40 @@ +# https://leetcode.com/problems/path-sum-ii/ + +# Time Complexity: O(n*h), O(n^2) worst case scenario +# Space Complexity: O(h*h) + +# This solution explores maintaining a currsum at every node and also creates +# a deep copy of the list passed on from the parent node and appends to this new list. + +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]: + res = [] + self.helper(root, 0, targetSum, [], res) + return res + + def helper(self, root, currSum, targetSum, path, res): + + if root == None: + return None + + currSum += root.val + currList = path.copy() + currList.append(root.val) + + if root.left is None and root.right is None: + if currSum == targetSum: + res.append(currList) + + self.helper(root.left, currSum, targetSum, currList, res) + self.helper(root.right, currSum, targetSum, currList, res) + + + + + \ No newline at end of file From 28e8aef59dd825a2fc8e3caa93c3dfcf87f89c9d Mon Sep 17 00:00:00 2001 From: Megha Raykar Date: Fri, 6 Mar 2026 17:44:03 -0800 Subject: [PATCH 2/2] Added Problem2 --- Problem2.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Problem2.py diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..e31005c1 --- /dev/null +++ b/Problem2.py @@ -0,0 +1,35 @@ +# https://leetcode.com/problems/symmetric-tree/description/ + +# Time Complexity: O(n) +# Space Complexity: O(n) + +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def isSymmetric(self, root: Optional[TreeNode]) -> bool: + + q = deque() + q.append(root.left) + q.append(root.right) + + while len(q) > 0: + left = q.popleft() + right = q.popleft() + + if left is None and right is None: + continue + elif left is None or right is None: + return False + elif left.val != right.val: + return False + + q.append(left.left) + q.append(right.right) + q.append(left.right) + q.append(right.left) + + return True