Rain Water Trapping program in Python

def rain_water_trap(arr):
    size = len(arr)
    left = size * [0]
    right = size * [0]
    left[0] = arr[0]
    max_so_far_left = arr[0]
    water = 0

    for index in range(size):
        if max_so_far_left < arr[index]:
            max_so_far_left = arr[index]
            left[index] = max_so_far_left
        else:
            left[index] = max_so_far_left
     
    max_so_far_right = arr[-1]

    for index in range(size-1, -1, -1):
        if max_so_far_right < arr[index]:
            max_so_far_right = arr[index]
            right[index] = max_so_far_right
        else:
            right[index] = max_so_far_right

    for index in range(size):
        water = water + min(left[index],right[index]) - arr[index]

    return water

list1 = [1, 0, 2, 0, 1, 0, 3, 1, 0, 2]
print(rain_water_trap(list1))

Evaluate a Postfix expression using Python

def postfix(arr):
    stack = []
    operators = ['+', '-', '*', '/', '%']
    for item in arr:
        if item not in operators:
            stack.append(item)
        else:
            first = int(stack.pop())
            second = int(stack.pop())

            if(item == '+'):
                stack.append(second + first)
            elif(item == '-'):
                stack.append(second - first)
            elif(item == '*'):
                stack.append(second * first)
            elif(item == '/'):
                stack.append(second / first)
            elif(item == '%'):
                stack.append(second % first)
    return stack[-1]

arr = ['2', '1', '+', '3', '5', '+', '*']
print(postfix(arr))

Return the minimum height of a Binary Tree using Python

class Binary:
    def __init__(self, key):
        self.data = key
        self.left = None
        self.right = None

def height(A):
    if A == None:
        return 0
    else:
        ldepth = height(A.left)
        rdepth = height(A.right)

        if(ldepth > rdepth):
            return 1+rdepth
        return 1+ldepth

root = Binary(1)
root.left = Binary(2)
root.right = Binary(3)
root.left.left = Binary(4)
root.left.right = Binary(5)
root.left.left.left = Binary(7)
root.right.right = Binary(8)

print(height(root))

Return the maximum height of a Binary Tree using Python

class Binary:
    def __init__(self, key):
        self.data = key
        self.left = None
        self.right = None

def height(A):
    if A == None:
        return 0
    else:
        ldepth = height(A.left)
        rdepth = height(A.right)

        if(ldepth > rdepth):
            return 1+ldepth
        return 1+rdepth

root = Binary(1)
root.left = Binary(2)
root.right = Binary(3)
root.left.left = Binary(4)
root.left.right = Binary(5)
root.left.left.left = Binary(7)
root.right.right = Binary(8)

print(height(root))