""" Outer view of a binary tree
    20
  8   22
5   3   25
  10  14
"""


class Node(object):
    def __init__(self, v):
        self.data = v
        self.left = None
        self.right = None


def left_view_without_leaves(root, currentLevel, levelTraversed):
    if not root:
        return
    if currentLevel > levelTraversed[0]:
        if root.left or root.right:
            print root.data,
        levelTraversed[0] = currentLevel
    left_view_without_leaves(root.left, currentLevel+1, levelTraversed)
    left_view_without_leaves(root.right, currentLevel+1, levelTraversed)


def right_view_without_leaves(root, currentLevel, levelTraversed):
    if not root:
        return
    if currentLevel > levelTraversed[0]:
        if root.left or root.right:
            print root.data,
        levelTraversed[0] = currentLevel
    right_view_without_leaves(root.right, currentLevel+1, levelTraversed)
    right_view_without_leaves(root.left, currentLevel + 1, levelTraversed)


def leaves(root):
    if not root:
        return
    if not root.left and not root.right:
        print root.data,
    leaves(root.left)
    leaves(root.right)


def outer_view(root):
    if root:
        print root.data,
        left_view_without_leaves(root.left, 0, [-1])
        leaves(root.left)
        leaves(root.right)
        right_view_without_leaves(root.right, 0, [-1])


if __name__ == '__main__':
    root = Node(20)
    root.left = Node(8)
    root.right = Node(22)
    root.left.left = Node(5)
    root.left.right = Node(3)
    root.left.right.left = Node(10)
    root.left.right.right = Node(14)
    root.right.right = Node(25)
    outer_view(root)
