From 9ab980a5eeb9424b71e973fba3edb06e2f864d9f Mon Sep 17 00:00:00 2001 From: Adithya Vinayak Date: Sat, 7 Mar 2026 23:55:05 -0500 Subject: [PATCH] working solution --- problem1.py | 26 ++++++++++++++++++++++++++ problem2.py | 24 ++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 problem1.py create mode 100644 problem2.py diff --git a/problem1.py b/problem1.py new file mode 100644 index 00000000..0d82e262 --- /dev/null +++ b/problem1.py @@ -0,0 +1,26 @@ +# problem 1 + +# 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 helper(self,root,targetSum,curr_sum,curr_arr): + if not root: + return + curr_sum += root.val + curr_arr.append(root.val) + if root.left is None and root.right is None: + if curr_sum == targetSum: + self.sum_arr.append(list(curr_arr)) + self.helper(root.left,targetSum,curr_sum,curr_arr) + self.helper(root.right,targetSum,curr_sum,curr_arr) + curr_arr.pop() + + def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]: + self.sum_arr = [] + self.helper(root,targetSum,0,[]) + return self.sum_arr \ No newline at end of file diff --git a/problem2.py b/problem2.py new file mode 100644 index 00000000..24f13931 --- /dev/null +++ b/problem2.py @@ -0,0 +1,24 @@ +# problem 2 +# 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 check(self,left_node,right_node): + if left_node and not right_node: + return False + if not left_node and right_node: + return False + if not left_node and not right_node: + return True + if left_node.val != right_node.val: + return False + return self.check(left_node.right,right_node.left) and self.check(left_node.left,right_node.right) + + def isSymmetric(self, root: Optional[TreeNode]) -> bool: + if not root: + return True + return self.check(root.left,root.right) \ No newline at end of file