From c01fb818d59ad7ec17687b24b830cc4d212776d1 Mon Sep 17 00:00:00 2001 From: allurkarsneha Date: Sat, 22 Aug 2026 01:13:38 -0500 Subject: [PATCH] Completed Leetcode 113 and 101 --- Problem 1- leetcode113.py | 36 ++++++++++++++++++++++++++++++++++++ Problem 2- Leetcode101.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 Problem 1- leetcode113.py create mode 100644 Problem 2- Leetcode101.py diff --git a/Problem 1- leetcode113.py b/Problem 1- leetcode113.py new file mode 100644 index 00000000..c35fe5e5 --- /dev/null +++ b/Problem 1- leetcode113.py @@ -0,0 +1,36 @@ +#Time Complexity: O(n) +#Space Complexity: O(n) where n is the number of nodes in the tree + +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution(object): + def pathSum(self, root, targetSum): + """ + :type root: Optional[TreeNode] + :type targetSum: int + :rtype: List[List[int]] + """ + self.result = [] + self.helper(root, targetSum, 0, []) + return self.result + + def helper(self, root, targetSum, currSum, path): + if root is None: + return + + currSum += root.val + path.append(root.val) + + if root.left is None and root.right is None: + if currSum == targetSum: + self.result.append(list(path)) + + self.helper(root.left, targetSum, currSum, path) + self.helper(root.right, targetSum, currSum, path) + + path.pop() + \ No newline at end of file diff --git a/Problem 2- Leetcode101.py b/Problem 2- Leetcode101.py new file mode 100644 index 00000000..8a32b3dc --- /dev/null +++ b/Problem 2- Leetcode101.py @@ -0,0 +1,30 @@ +#Time Complexity: O(n) +#Space Complexity: O(h) where h is the height of the tree + +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution(object): + def isSymmetric(self, root): + """ + :type root: Optional[TreeNode] + :rtype: bool + """ + return self.helper(root.left, root.right) + + def helper(self, left, right): + if left is None and right is None: + return True + + if left is None or right is None: + return False + + if left.val != right.val: + return False + + return (self.helper(left.left, right.right) and + self.helper(left.right, right.left)) + \ No newline at end of file