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