"""  1.	Design a binary tree and then write an algorithm to print the least(nearest) two common parent
     (if 2 parents exist otherwise 1  common parent) node between 2 nodes of a binary tree for
     given 2 key values which are present in binary tree
"""

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

def findPath(root, path, k):
    if root is None:
        return False

    path.append(root.key)

    if root.key == k:
        return True
    if ((root.left != None and findPath(root.left, path, k)) or
            (root.right != None and findPath(root.right, path, k))):
        return True
    path.pop()
    return False

def main(root, n1, n2):
    path1 = []
    path2 = []

    if (not findPath(root, path1, n1) or not findPath(root, path2, n2)):
        return -1

    i = 0
    while (i < len(path1) and i < len(path2)):
        if path1[i] != path2[i]:
            break
        i += 1
    return path1[i - 1]

root = Node(2)
root.left = Node(1)
root.right = Node(3)
root.right.left = Node(4)
root.right.right = Node(5)
root.right.right.right = Node(6)

print(main(root, 1,5))
print(main(root, 3,6))
print(main(root, 4,6))