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