Invert Binary Tree or Mirror Tree | Tree Data Structure playlist C++ | Hello World | Leetcode

  Рет қаралды 9,090

Hello World

Hello World

Күн бұрын

This is the video under the series of DATA STRUCTURE & ALGORITHM in a TREE Playlist. We are going to understand How to create Invert Binary Tree or Mirror Tree.
Join My Telegram channel for more Updates: telegram.me/he...
complete DSA preparation: github.com/Pri...
► 226. Invert Binary Tree || Mirror Tree
Given the root of a binary tree, invert the tree, and return its root.
Input:
1
/ \
2 3
Output: 2 1 3
Explanation: The tree is
1 (mirror) 1
/ \ = / \
3 2 2 3
The inorder of mirrors is 2 1 3
We also Provide courses on Competitive Programming and Data structure and Algorithms. Please see our Full Playlist on our Channel.
----------------------------------------------------------------------------------------:
Invert Binary Tree: leetcode.com/p...
code in This Video: github.com/Pri...
----------------------------------------------------------------------------------------
** Unacademy
Use my code "HELLOWORLD" to get 10% discount
Career as a Developer: unacademy.com/...
Top interview questions: bit.ly/Unacade...
----------------------------------------------------------------------------------------
*Follow me *
LinkedIn► / iamprince
Facebook► / helloworldofficials
Instagram► / helloworldbyprince
Twitter► / prince_king_
Telegram► telegram.me/he...
----------------------------------------------------------------------------------------
►Our Playlists on:-
► Tree: • Tree Data Structure & ...
► Hashing: • Hashing Data Structure...
► Matrix: • Matrix (Multidimension...
► STL: • Standard Template Libr...
► Leetcode: • LeetCode Solutions And...
►Competitive Programming: • Full course in Competi...
►C++ Full Course : • C++ full Course in HINDI
►Algorithms: • L-01 || Prefix Sum Arr...
►Data Structure: • Data Structures with C...
------------------------------------------------------------------------
🌟 Please leave a LIKE ❤️ and SUBSCRIBE for more AMAZING content! 🌟
✨ Tags ✨
Invert Binary Tree
Mirror Tree
The tree Data structure in Hindi
Find the Invert of a Binary Tree
Use of Tree Data structure in real Life
question asked in Google
off-campus placement
how to learn to code for beginners
Practice Tree data structure
tree in data structure
Best Telegram channel for Off-campus Placement drive
Tree in a data structure in Hindi
Tree Full playlist for Beginners
#Tree #Leetcode #programming

Пікірлер: 67
@349_rahulbhattacharya6
@349_rahulbhattacharya6 2 жыл бұрын
mast explanation!!Mai directly solution dekhna pda..nhi soch paya tha solution..
@SurajKumar-nz7dh
@SurajKumar-nz7dh 3 жыл бұрын
I tried it using queue and got same output. Awesome Video! Thanks a lot...
@Playlist-cj9ct
@Playlist-cj9ct 7 ай бұрын
I did it, so happy that I solved it before watching video.
@udaytewary3809
@udaytewary3809 2 жыл бұрын
Really thankyou bhaiya aapka samjhane ka tarika aisa h ki bhaut lucid way ya Asan way me samaj me aya although maine level order se socha tha ki value ko swap karu pr kaise karu samaj me nhi aa raha tha but apke stack wale solution se idea aa gya mujhe iske liye dil se dhanyawad bhaiya😍😍❤️‍🔥❤️‍🔥❤️‍🔥
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Thanks to u for this response Please, share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀
@tusharrastogi7490
@tusharrastogi7490 2 жыл бұрын
Just played the video and you insisted to try it on your own, and yeah.. its done. 1 like for that
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Waooo thanks meri baat maankar question solve karne ke liye nice work
@divyansh1391
@divyansh1391 2 жыл бұрын
sb samjh aa jata hai bhaiya....
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
waoo yaar i am happy
@amalasebastian9968
@amalasebastian9968 2 жыл бұрын
Great video ,thank you so much sir ,do not leave teaching!
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Just support me I will never leave teaching
@gokulnaathb2627
@gokulnaathb2627 2 жыл бұрын
Samjh m aa rha h, bhaiya, thank you so much
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Your welcome dear
@ashwinvarma9349
@ashwinvarma9349 3 жыл бұрын
Good to see you again😄
@HelloWorldbyprince
@HelloWorldbyprince 3 жыл бұрын
😊
@nopecharon
@nopecharon 2 жыл бұрын
Your explanation is best
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Thanks a lot 😊
@priyabansal9911
@priyabansal9911 3 жыл бұрын
Thank you Prince for the video and hope you are fine now.
@HelloWorldbyprince
@HelloWorldbyprince 3 жыл бұрын
All good now Thanks or asking
@rajchinagundi7498
@rajchinagundi7498 2 жыл бұрын
class Solution { public: TreeNode* invertTree(TreeNode* root) { if(root!=NULL){ swap(root->left,root->right); invertTree(root->left); invertTree(root->right); } return root; } }; I did it by this way
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
amazing bro
@anonymous090
@anonymous090 2 жыл бұрын
Thank you Bhaiya, ache se samajh agya
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Thanks yaar aise tumhara name kya hai ?
@anonymous090
@anonymous090 2 жыл бұрын
@@HelloWorldbyprince Rashi Shukla 😄
@codewith_me07
@codewith_me07 Ай бұрын
Sir we can swap directly left and right then call left and right return roo
@Alokkumar-xu8sf
@Alokkumar-xu8sf 3 жыл бұрын
aacha laga
@memefulland7140
@memefulland7140 2 жыл бұрын
Done using link swaping Code: TreeNode* invertTree(TreeNode* root) { if(!root) return root; TreeNode* nl=root->left; TreeNode* nr=root->right; root->left=nr; root->right=nl; invertTree(root->left); invertTree(root->right); return root; }
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Yeaaahhhh 🔥🔥🔥🔥🔥
@swastikkumar9777
@swastikkumar9777 2 жыл бұрын
Loved the way you explained.
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Thanks a lot 😊
@shaantyagi2187
@shaantyagi2187 2 жыл бұрын
C++ solution: void invert(TreeNode* root){ if(root==NULL){ return; } // jaise hum log bubble sort main swap karte the ki ek temp variable bana ke usme value // rakh lete the waise hi yahan pe kar sakte hai TreeNode* left = root->left; root->left = root->right; root->right = left; invert(root->left); invert(root->right); } TreeNode* invertTree(TreeNode* root) { invert(root); return root; }
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Amazing 😻 brother
@AnkitSingh-nm5wz
@AnkitSingh-nm5wz 3 жыл бұрын
Great video...really helped me!
@HelloWorldbyprince
@HelloWorldbyprince 3 жыл бұрын
Thanks for the support 💗
@Sheer_life9980
@Sheer_life9980 2 жыл бұрын
wow osm way..
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Yup, thanks buddy 😊
@manastripathi1157
@manastripathi1157 Жыл бұрын
Another approach without using stack class Solution { public: TreeNode* invertTree(TreeNode* root) { if(root==NULL) return NULL; invertTree(root->left); invertTree(root->right); swap(root->left,root->right); return root; } };
@cr7johnChan
@cr7johnChan 2 жыл бұрын
//My solution for recursion if(root==NULL)return root; TreeNode*temp=root->left; root->left=root->right; root->right=temp; root->left=invertTree(root->left); root->right=invertTree(root->right); return root;
@Hackkrosuruaatse
@Hackkrosuruaatse Ай бұрын
Bro placement ho gyii aapki?
@masudalammolla3400
@masudalammolla3400 Жыл бұрын
If we use level order traversal approcah and every level before push it to the ans vector if we do reverse of the temp vector then push it
@TheStarboyVlog
@TheStarboyVlog Жыл бұрын
sir yeh question sayad queue se banega stack ki jagha
@BAkashAnand
@BAkashAnand Жыл бұрын
Recursive Solution: TreeNode* invertTree(TreeNode* root) { if(root!=NULL){ swap(root->left,root->right); invertTree(root->left); invertTree(root->right); } return root; }
@shashwatsingh253
@shashwatsingh253 2 жыл бұрын
Bhaiya I did by the help of Level Order Traversal class Solution { public: TreeNode* invertTree(TreeNode* root) { if(!root) return root; queue q; q.push(root); while(!q.empty()) { TreeNode* ptr = q.front(); q.pop(); if(ptr->left && !ptr->right || !ptr->left && ptr->right || ptr->left && ptr->right) { swap(ptr->left,ptr->right); } if(ptr->left) q.push(ptr->left); if(ptr->right) q.push(ptr->right); } return root; } };
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Amazing 🤩🤩
@ayushnamdev6326
@ayushnamdev6326 3 жыл бұрын
bhiya full samajh aara h
@sohebshaikh3344
@sohebshaikh3344 3 жыл бұрын
Honestly Bhaiya maine 2 no approch socha tha vector wala aur swap wala bhi vector run bhi kiya aur samjh gya wo galat but in Swap code m baar baar error mil rha tha
@SanjaySingh-qf4tk
@SanjaySingh-qf4tk Жыл бұрын
if(root==NULL){ return root; } invertTree(root->left); invertTree(root->right); if(root->left!=NULL|| root->right!=NULL){ swap(root->left,root->right); } return root;
@NITianMonika
@NITianMonika Жыл бұрын
Simple think about first three node baki sb recursion kar dega: 6line code in c++
@NITianMonika
@NITianMonika Жыл бұрын
class Solution { private: public: TreeNode* invertTree(TreeNode* root) { if(root == NULL) return root; invertTree(root->right); invertTree(root->left); swap(root->right, root->left); return root; } };
@AnkitKumar-ys7vs
@AnkitKumar-ys7vs Жыл бұрын
Stack (LIFO) can't used there, Queue(FIFO) will be used.
@HelloWorldbyprince
@HelloWorldbyprince Жыл бұрын
Ohh i see, and can you pls explain me how ?
@jayanth1191
@jayanth1191 3 жыл бұрын
Sir at 12:21 top must be 3 isn't it? Please explain it sir
@satyamsrivastava1871
@satyamsrivastava1871 2 жыл бұрын
Bhaiya jab humne sara node me store krawaya toh v root me kaise change hau?
@ramanpreetsingh5677
@ramanpreetsingh5677 3 жыл бұрын
Bhai, your content is amazing no one did it with stacks on YT
@HelloWorldbyprince
@HelloWorldbyprince 3 жыл бұрын
Your welcome Raman 🤠
@nitinchauhan_18
@nitinchauhan_18 Жыл бұрын
Using Stack but doing FIFO
@HelloWorldbyprince
@HelloWorldbyprince Жыл бұрын
Stack is LIFO
@R_yuk-xd
@R_yuk-xd 2 жыл бұрын
sorry but agar stack se pop krenge to last element phle pop hoga shayad.
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Yesss u r right buddy
@mayankchoudhary7937
@mayankchoudhary7937 Жыл бұрын
it doesn't matter in what it pops.. it was trick to explain i guess, try push in stack in reverse order like right first then left and dry run it..
@franky1598
@franky1598 2 жыл бұрын
bhaiya yeh approchh kaise hai class Solution { public: TreeNode* invertTree(TreeNode* root) { if(root==NULL) return NULL ; TreeNode * left = root->left ; TreeNode * right = root->right; root->left = right; root->right = left ; invertTree(root->left); invertTree(root->right); return root ; } }; vaise bhaiya mai bhi nit hamirpur se hu apse kuch guidance chaiye
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
badhiya hai bro , please we can connect on Linkedin
@gauravnavinchandrabadgujar4057
@gauravnavinchandrabadgujar4057 2 жыл бұрын
if(!st.empty()) ki jagah while poop aaegi. typo mistake.
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
Thanks buddy
@askshivansh
@askshivansh 2 жыл бұрын
Bhaiya, mai address ko swap kr rha hu, ye solution bhi valid hai na?
@HelloWorldbyprince
@HelloWorldbyprince 2 жыл бұрын
yup
А я думаю что за звук такой знакомый? 😂😂😂
00:15
Денис Кукояка
Рет қаралды 4,7 МЛН
Из какого города смотришь? 😃
00:34
МЯТНАЯ ФАНТА
Рет қаралды 2,6 МЛН
Binary Tree in Data Structures | All about Binary Tree | DSA Course
1:22:13
L28. Maximum Width of Binary Tree | C++ | Java
22:41
take U forward
Рет қаралды 275 М.
Machine Learning for Everybody - Full Course
3:53:53
freeCodeCamp.org
Рет қаралды 8 МЛН
C Programming Tutorial for Beginners
3:46:13
freeCodeCamp.org
Рет қаралды 14 МЛН
Invert a binary tree | Leetcode 226 | Easy | C++ | Code + Explanation
6:53
L16. Diameter of Binary Tree | C++ | Java
13:47
take U forward
Рет қаралды 379 М.