""" 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()
""" 0543.1 - Diameter of Binary Tree - Solution 1 - DFS """
#####################################################################################
# Imports
#####################################################################################
from typing import Optional
#####################################################################################
# Classes
#####################################################################################
class TreeNode:
"""Tree Node Class"""
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
"""Solution Class"""
def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
"""Diameter of Binary Tree Function"""
self.diameter = 0
def depth(node: Optional[TreeNode]) -> int:
if not node:
return 0
left = depth(node.left)
right = depth(node.right)
# Update diameter: path through this node = left depth + right depth
self.diameter = max(self.diameter, left + right)
# Return depth of this subtree
return 1 + max(left, right)
depth(root)
return self.diameter
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
# Example 1: root = [1,2,3,4,5] -> Output: 3
root1 = TreeNode(1)
root1.left = TreeNode(2)
root1.right = TreeNode(3)
root1.left.left = TreeNode(4)
root1.left.right = TreeNode(5)
print(Solution().diameterOfBinaryTree(root1)) # 3
# Example 2: root = [1,2] -> Output: 1
root2 = TreeNode(1)
root2.left = TreeNode(2)
print(Solution().diameterOfBinaryTree(root2)) # 1
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0543.2 - Diameter of Binary Tree - Solution 2 - Iterative Post-Order """
#####################################################################################
# Imports
#####################################################################################
from typing import Optional
#####################################################################################
# Classes
#####################################################################################
class TreeNode:
"""Tree Node Class"""
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
"""Solution Class"""
def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
"""Diameter of Binary Tree Function"""
if not root:
return 0
diameter = 0
depth_map = {None: 0}
stack = [(root, False)]
while stack:
node, visited = stack.pop()
if visited:
left_depth = depth_map.get(node.left, 0)
right_depth = depth_map.get(node.right, 0)
diameter = max(diameter, left_depth + right_depth)
depth_map[node] = 1 + max(left_depth, right_depth)
else:
stack.append((node, True))
if node.right:
stack.append((node.right, False))
if node.left:
stack.append((node.left, False))
return diameter
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
# Example 1: root = [1,2,3,4,5] -> Output: 3
root1 = TreeNode(1)
root1.left = TreeNode(2)
root1.right = TreeNode(3)
root1.left.left = TreeNode(4)
root1.left.right = TreeNode(5)
print(Solution().diameterOfBinaryTree(root1)) # 3
# Example 2: root = [1,2] -> Output: 1
root2 = TreeNode(1)
root2.left = TreeNode(2)
print(Solution().diameterOfBinaryTree(root2)) # 1
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()