Invert Binary Tree | Leetcode #226

preview_player
Показать описание
This video explains a very basic recursion type problem which is frequently asked in interviews which is to find the mirror image of a given binary tree.Mirror image of a binary tree is also called as inversion of a binary tree.Solving this problem by constructing a new binary tree is not a correct approach.We need to just change the pointers and invert the binary tree inplace. I have explained the inplace binary tree inversion algorithm using recursion by showing proper examples. At the end of the video, i have also shown the code walkthrough for the inversion algo. CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)

=================================================================
=================================================================

SIMILAR PROBLEM:-
Рекомендации по теме
Комментарии
Автор

Earlier thought the toughest, but now it seems the easiest. Thank you YouTuber.

prateekmahajan
Автор

The way you explain is amazing..you made the problem cakewalk.a big thanks to you.😅

surbhitamrakar
Автор

This question I was seen in Amazon interview 😊.. This solutions is such a beautiful and in a well maintained way☺️😇 you are explaining.. thank you sir to give us solutions..

kunalsoni
Автор

Nice logic. We can also use pre order and keep on changing the left and the right pointers while traversing the the tree, but your logic is more enjoyable.

anuragsoni
Автор

Great video. Definitely made it super easy to understand compared to those big youtubers. Thanks!

pooblock
Автор

time complexity = O(n) as traversal is used
Space complexity - O(n) internal stack space

pranavsharma
Автор

Thank you so much for the detailed explanation of how recursion works here :)

abantikatonny
Автор

adding another check may help avoid the unnecessary null swapping at the root level. :)

amitavamozumder
Автор

This isn’t actually a mirror image, it’s a reflection on the y axis, but great video!
Solution:
Doing a post order traversal (visit left node, visit right node, then visit root) when we finally visit the root (after recursion) we switch the pointers, left node becomes right and right becomes left

mostinho
Автор

Thanks for this... Do any swaps happen at 1:35? I mean that does our program actually get to swap the None values on the root node of 1?

ottyudo
Автор

man you are awesome ....hope we will see more lectures coming from you

tanmay
Автор

sir plz also make a playlist companies wise technical interview problems

anmolwadali
Автор

You can also do this using PreOrder traversal.

ryanaugustine
Автор

Java solution:

class Solution {
public TreeNode invertTree(TreeNode root) {

if(root == null) return null;
invertTree(root.left);
invertTree(root.right);
swapNodes(root);

return root;
}

public void swapNodes(TreeNode root) {
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
}
}

ebrahimbonger
Автор

Thanks!..I want to clarify one thing that will this code also swap the null values of the leaf node(when leaf node becomes the root or curr)?

meghamalviya
Автор

expecting recursion process in more details but overall i get the logic; thanks

Learner
Автор

Thanks for the video. Mind if I ask what mic you are using to record?

cloud
Автор

Sir will the space complexity be O(d) where d is the depth of the Binary Tree or O(n)?

agileprogramming
Автор

TechDose If you are launching any course. What will be the problem in telling course fees how much you charged in the last batch? At least someone can't wait for your sweet time to respond right?

ferringxor
Автор

Java solution
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null)
return root;
else
{
invertTree(root.left);
invertTree(root.right);
TreeNode t;
t=root.left;
root.left=root.right;
root.right=t;
}
return root;
}
}

md_aaqil
visit shbcf.ru