1
+ # https://practice.geeksforgeeks.org/problems/mirror-tree/1/
2
+
3
+
4
+
5
+ '''
6
+ class Node:
7
+ def _init_(self, val):
8
+ self.right = None
9
+ self.data = val
10
+ self.left = None
11
+ '''
12
+ # your task is to complete this function
13
+
14
+ class Solution :
15
+ #Function to convert a binary tree into its mirror tree.
16
+ def createNode (self , val ):
17
+ mirror = Node (0 )
18
+ mirror .data = val
19
+ mirror .right = None
20
+ mirror .left = None
21
+
22
+
23
+ def mirror (self ,root ):
24
+
25
+ # Code here
26
+ if (root == None ):
27
+ return
28
+ else :
29
+ temp = root
30
+ self .mirror (root .left )
31
+ self .mirror (root .right )
32
+ temp = root .left
33
+ root .left = root .right
34
+ root .right = temp
35
+
36
+ return root
37
+
38
+
39
+
40
+ #{
41
+ # Driver Code Starts
42
+ #Initial Template for Python 3
43
+
44
+
45
+
46
+ #Contributed by Sudarshan Sharma
47
+ from collections import deque
48
+ # Tree Node
49
+ class Node :
50
+ def __init__ (self , val ):
51
+ self .right = None
52
+ self .data = val
53
+ self .left = None
54
+
55
+
56
+ def inorderTraversalUtil (root ):
57
+ # Code here
58
+ if root is None :
59
+ return
60
+ inorderTraversalUtil (root .left )
61
+ print (root .data , end = ' ' )
62
+ inorderTraversalUtil (root .right )
63
+
64
+ def inorderTraversal (root ):
65
+ # Code here
66
+ inorderTraversalUtil (root )
67
+ print ()
68
+
69
+ # Function to Build Tree
70
+ def buildTree (s ):
71
+ #Corner Case
72
+ if (len (s )== 0 or s [0 ]== "N" ):
73
+ return None
74
+
75
+ # Creating list of strings from input
76
+ # string after spliting by space
77
+ ip = list (map (str ,s .split ()))
78
+
79
+ # Create the root of the tree
80
+ root = Node (int (ip [0 ]))
81
+ size = 0
82
+ q = deque ()
83
+
84
+ # Push the root to the queue
85
+ q .append (root )
86
+ size = size + 1
87
+
88
+ # Starting from the second element
89
+ i = 1
90
+ while (size > 0 and i < len (ip )):
91
+ # Get and remove the front of the queue
92
+ currNode = q [0 ]
93
+ q .popleft ()
94
+ size = size - 1
95
+
96
+ # Get the current node's value from the string
97
+ currVal = ip [i ]
98
+
99
+ # If the left child is not null
100
+ if (currVal != "N" ):
101
+
102
+ # Create the left child for the current node
103
+ currNode .left = Node (int (currVal ))
104
+
105
+ # Push it to the queue
106
+ q .append (currNode .left )
107
+ size = size + 1
108
+ # For the right child
109
+ i = i + 1
110
+ if (i >= len (ip )):
111
+ break
112
+ currVal = ip [i ]
113
+
114
+ # If the right child is not null
115
+ if (currVal != "N" ):
116
+
117
+ # Create the right child for the current node
118
+ currNode .right = Node (int (currVal ))
119
+
120
+ # Push it to the queue
121
+ q .append (currNode .right )
122
+ size = size + 1
123
+ i = i + 1
124
+ return root
125
+
126
+
127
+ if __name__ == "__main__" :
128
+ t = int (input ())
129
+ for _ in range (0 ,t ):
130
+ s = input ()
131
+ root = buildTree (s )
132
+ Solution ().mirror (root )
133
+ inorderTraversal (root )
134
+
135
+
136
+
137
+ # } Driver Code Ends
0 commit comments