# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
# Assignment - Check if two N-ary trees are Mirror image or not
# Time: O(n)
# Space: O(h)

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

def preorder(root):
    if not root:    return root
    print(root.val, end = ' ')
    preorder(root.left)
    preorder(root.right)

def mirror_image(root):
    if not root:    return root
    left = mirror_image(root.left)
    right = mirror_image(root.right)
    root.left = right
    root.right = left
    return root

if __name__=='__main__':
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    root.right.left = Node(4)
    root.right.right = Node(5)
    preorder(root)
    root = mirror_image(root)
    print()
    preorder(root)
# -



StarcoderdataJupyterScriptsDedupFiltered