Invert a Binary Tree
You are provided with a Binary Tree and one of its leaf nodes. You have to invert this binary tree. Inversion must be done by following all the below guidelines:
• The given leaf node becomes the root after the inversion.
• For a node (say, ‘x’)
○ If there exists the left child that is not yet taken then this child must become the right child of ‘x’.
○ If the left child is already taken then the right child is still on the right side of ‘x’.
• The parent of ‘x’ must be the left child of ‘x’.
For Example:
Consider the above binary tree (image- before inversion), if the given leaf node is ‘1’ then the binary tree after inversion becomes exactly the same as given in the image representing after inversion.
Note:
The given binary tree will only have distinct values of nodes.
Input Format:
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case will contain a single TreeNode “leaf” which denotes one of the leaf nodes of the given binary tree.
The second line of each test case contains elements of the tree in the level order form. The line consists of values of nodes separated by a single space. In case a node is null, we take -1 in its place.
For example, the input for the tree depicted in the below image would be :
1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1
Explanation :
Level 1 :
The root node of the tree is 1
Level 2 :
Left child of 1 = 2
Right child of 1 = 3
Level 3 :
Left child of 2 = 4
Right child of 2 = null (-1)
Left child of 3 = 5
Right child of 3 = 6
Level 4 :
Left child of 4 = null (-1)
Right child of 4 = 7
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 6 = null (-1)
Right child of 6 = null (-1)
Level 5 :
Left child of 7 = null (-1)
Right child of 7 = null (-1)
The first not-null node(of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.
The input ends when all nodes at the last level are null(-1).
Note :
The above format was just to provide clarity on how the input is formed for a given tree.
The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output Format:
For each test case, return the whole binary tree (according to the format as it has been given in the input) after considering the given leaf node as the new root node.
Output for every test case will be printed in a separate line.
Note:
You don’t need to print anything; It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 50
1 <= N <= 10000
-10^5 <= DATA <= 10^5
Leaf is one of the leaf nodes of the given binary tree.
Time limit: 1 sec
CodingNinjas
author
2y
I explained the approach of swaping the child two nodes while traversing each node. The interview was fine with that approach
CodingNinjas
author
2y
In-order Traversal
The basic idea is to first fill all the nodes (in a stack) that are present in a path whose leaf node is the given leaf node using In-order traversal. And, then keep removing nodes f...read more
Help your peers!
Add answer anonymously...
Top Freshworks Graduate Trainee interview questions & answers
Popular interview questions of Graduate Trainee
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app