""" 0001.1 - Solution 1 - Brute Force Approach """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def twoSum(self, nums: List[int], target: int) -> List[int]:
"""Two Sum Function"""
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
if nums[i] + nums[j] == target:
return [i, j]
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().twoSum([3, 3], 6))
print(Solution().twoSum([3, 2, 4], 6))
print(Solution().twoSum([2, 7, 11, 15], 9))
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0001.1 - Solution 1 - Using Enumerate """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def twoSum(self, nums: List[int], target: int) -> List[int]:
"""Two Sum Function"""
for key1, num1 in enumerate(nums):
for key2, num2 in enumerate(nums[key1 + 1 :], key1 + 1):
# enumerate(iterable, start=1) -> start value determines starting index
# Printing value and key will output 1 i , 2 j, 3 k, 4 l
if num1 + num2 == target:
return [key1, key2]
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().twoSum([3, 3], 6))
print(Solution().twoSum([3, 2, 4], 6))
print(Solution().twoSum([2, 7, 11, 15], 9))
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0257.1 - Binary Tree Paths - Solution 1 - Recursive DFS """
#####################################################################################
# Imports
#####################################################################################
from typing import List, Optional
#####################################################################################
# Classes
#####################################################################################
class TreeNode:
"""TreeNode Class"""
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
"""Solution Class"""
def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:
"""Binary Tree Paths Function"""
result = []
def dfs(node: Optional[TreeNode], path: str) -> None:
if not node:
return
path += str(node.val)
if not node.left and not node.right:
result.append(path)
else:
path += "->"
dfs(node.left, path)
dfs(node.right, path)
dfs(root, "")
return result
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
# Example 1: root = [1,2,3,null,5] -> ["1->2->5","1->3"]
root1 = TreeNode(1, TreeNode(2, None, TreeNode(5)), TreeNode(3))
print(Solution().binaryTreePaths(root1))
# Example 2: root = [1] -> ["1"]
root2 = TreeNode(1)
print(Solution().binaryTreePaths(root2))
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0257.2 - Binary Tree Paths - Solution 2 - Iterative DFS using Stack """
#####################################################################################
# Imports
#####################################################################################
from typing import List, Optional
#####################################################################################
# Classes
#####################################################################################
class TreeNode:
"""TreeNode Class"""
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
"""Solution Class"""
def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:
"""Binary Tree Paths Function"""
if not root:
return []
result = []
stack = [(root, str(root.val))]
while stack:
node, path = stack.pop()
if not node.left and not node.right:
result.append(path)
if node.right:
stack.append((node.right, path + "->" + str(node.right.val)))
if node.left:
stack.append((node.left, path + "->" + str(node.left.val)))
return result
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
# Example 1: root = [1,2,3,null,5] -> ["1->2->5","1->3"]
root1 = TreeNode(1, TreeNode(2, None, TreeNode(5)), TreeNode(3))
print(Solution().binaryTreePaths(root1))
# Example 2: root = [1] -> ["1"]
root2 = TreeNode(1)
print(Solution().binaryTreePaths(root2))
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()