Invert Binary Tree (LeetCode 226) | Full solution with diagrams and animations | Study Algorithms

Nikhil Lohia β€’ December 18, 2022
Video Thumbnail
Nikhil Lohia Logo

Nikhil Lohia

View Channel

About

I want to share with you my knowledge, that I have gained all my life. I like to see every task in your day to day life as an interesting problem to solve, and if you can understand the requirements, solving for it can become somewhat intuitive. I try to explore certain aspects of day to day life and how I can make them easier for you. This involves daily hacks, tutorials, innovations, new discoveries, experiences and all the stuff I find amusing. As a software engineer myself, I also believe that good problem solving skills can help you overcome most of the barriers in life. Programming is one of them. If you know how to approach a problem, writing a code for it in any language could be a very typical task. With this channel I aim to focus on the way to solve a problem efficiently rather than just its implementation.

Video Description

Inverting a binary tree is more synonymous with mirroring a binary tree. Try to assume that there is a mirror in front of the tree and you need to return the reflection. This video explains beautifully with diagrams and animations how the reflection looks like and how to create one. All of this is done using a level order traversal technique. Actual Problem: https://leetcode.com/problems/invert-binary-tree/ Chapters: 00:00 - Intro 00:58 - Problem Statement and Description 03:15 - The thought behind the approach 05:44 - Recursive Solution 08:56 - Level Order Traversal Trick 12:05 - Dry-run of Code 14:28 - Final Thoughts πŸ“š Links to topics I talk about in the video: LeetCode Problems: https://www.youtube.com/playlist?list=PLFdAYMIVJQHPaldBFQXzd59EiqfJTYkah Level Order Traversal: https://youtu.be/9M3oCwPGamE Post Order Traversal: https://youtu.be/ZIXGxg8ZLKs Tree Data Structure: https://youtu.be/6jwUjPs27Pc πŸ“˜ A text based explanation is available at: https://studyalgorithms.com Code on Github: https://github.com/nikoo28/java-solutions/blob/master/src/main/java/leetcode/easy/InvertTree.java Test-cases on Github: https://github.com/nikoo28/java-solutions/blob/master/src/test/java/leetcode/easy/InvertTreeTest.java πŸ“– Reference Books: Starting Learn to Code: https://amzn.to/36pU0JO Favorite book to understand algorithms: https://amzn.to/39w3YLS Favorite book for data structures: https://amzn.to/3oAVBTk Get started for interview preparation: https://amzn.to/39ysbkJ πŸ”— To see more videos like this, you can show your support on: https://www.buymeacoffee.com/studyalgorithms πŸŽ₯ My Recording Gear: Recording Light: https://amzn.to/3pAqh8O Microphone: https://amzn.to/2MCX7qU Recording Camera: https://amzn.to/3alg9Ky Tablet to sketch and draw: https://amzn.to/3pM6Bi4 Surface Pen: https://amzn.to/3pv6tTs Laptop to edit videos: https://amzn.to/2LYpMqn πŸ’» Get Social πŸ’» Follow on Facebook at: https://www.facebook.com/studyalgos Follow on Twitter at: https://www.twitter.com/studyalgorithms Follow on Tumblr at: https://studyalgos.tumblr.com/ Subscribe to RSS feeds: https://studyalgorithms.com/feed/ Join fan mail: http://eepurl.com/g9Dadv #leetcode #programming #interview

You May Also Like

Essential Camera Gear Upgrades

AI-recommended products based on this video

Loading...
COTUBLR 31 Inch Computer Desk, Home Office Desk, Simple Modern Small Desk for Bedroom, Writing Desk with Storage Bag, Study Table for Students, Grey Oak

COTUBLR 31 Inch Computer Desk, Home Office Desk, Simple Modern Small Desk for Bedroom, Writing Desk with Storage Bag, Study Table for Students, Grey Oak

(180)
$64.99
FREE delivery Thu, Jun 26
Loading...
SmallRig ZV-E1 Camera Baseplate for Sony ZV-E1, Built-in Cold Shoe Mount and QR Plate (for Arca) Quick Switch Between Tripod and Stabilizer for DJI RS 2 / RSC 2 / RS 3 / RS 3 Pro - 4314

SmallRig ZV-E1 Camera Baseplate for Sony ZV-E1, Built-in Cold Shoe Mount and QR Plate (for Arca) Quick Switch Between Tripod and Stabilizer for DJI RS 2 / RSC 2 / RS 3 / RS 3 Pro - 4314

(3)
$44.99
FREE delivery Thu, Jun 19
Loading...
SmallRig FX3 / FX30 HawkLock Quick Release Camera Cage Kit for Sony with Cable Clamp, Bulit-in QR Cold Shoe NATO Rail/QR Plate for Arca, Modular Design Compatible with Original XLR Handle - 4770

SmallRig FX3 / FX30 HawkLock Quick Release Camera Cage Kit for Sony with Cable Clamp, Bulit-in QR Cold Shoe NATO Rail/QR Plate for Arca, Modular Design Compatible with Original XLR Handle - 4770

(35)
$124.94
FREE delivery Thu, Jun 19
Loading...
SmallRig FX3 / FX30 HawkLock Quick Release Camera Cage Kit for Sony with XLR Handle Extension, Wrist Strap and Cable Clamp for HDMI, QR Cold Shoe, NATO Rail/QR Plate for Arca,Titanium Gray - 4771

SmallRig FX3 / FX30 HawkLock Quick Release Camera Cage Kit for Sony with XLR Handle Extension, Wrist Strap and Cable Clamp for HDMI, QR Cold Shoe, NATO Rail/QR Plate for Arca,Titanium Gray - 4771

(72)
$174.24$162.04
FREE delivery Thu, Jun 19
Loading...
AC Power Cord Cable Fit for Sony PS5 PS4 PS3 Slim PlayStation 5 4 3, Xbox Series X/S, Xbox One X/S, TV, Printer Universal 2 Prong Supply Replacement - (ETL Listed Cable)

AC Power Cord Cable Fit for Sony PS5 PS4 PS3 Slim PlayStation 5 4 3, Xbox Series X/S, Xbox One X/S, TV, Printer Universal 2 Prong Supply Replacement - (ETL Listed Cable)

(5,351)
$8.49
Prime
500+ bought in past month
Loading...
Anker USB C Charger Cable (6ft 100W, 2Pack), USB 2.0 Type C Fast Charging Cable for iPhone 15 / 15Pro / 15Plus / 15ProMax MacBook Pro 2020, iPad Pro 2020, iPad Air 4, Samsung Galaxy S23+/S23 Ultra ClimatePartner certified

Anker USB C Charger Cable (6ft 100W, 2Pack), USB 2.0 Type C Fast Charging Cable for iPhone 15 / 15Pro / 15Plus / 15ProMax MacBook Pro 2020, iPad Pro 2020, iPad Air 4, Samsung Galaxy S23+/S23 Ultra ClimatePartner certified

(44,904)
$17.99
FREE delivery Wed, Aug 20 on your first order
6K+ bought in past month
Loading...
Anker USB C Cable, [2-Pack, 6 ft] Type C Charger Premium Nylon USB Cable, USB A to Type C Charging Cable Fast Charge for Samsung Galaxy S10 S10+ / Note 8, LG V20 and Other USB C Charger (Black)

Anker USB C Cable, [2-Pack, 6 ft] Type C Charger Premium Nylon USB Cable, USB A to Type C Charging Cable Fast Charge for Samsung Galaxy S10 S10+ / Note 8, LG V20 and Other USB C Charger (Black)

(103,365)
$13.49
5K+ bought in past month
Loading...
SanDisk 64GB Extreme PRO SDXC UHS-I Memory Card - C10, U3, V30, 4K UHD, SD Card - SDSDXXU-064G-GN4IN

SanDisk 64GB Extreme PRO SDXC UHS-I Memory Card - C10, U3, V30, 4K UHD, SD Card - SDSDXXU-064G-GN4IN

(71,074)
$21.99$16.59
FREE delivery Sun, Aug 10 on your first order
500+ bought in past month
Loading...
SanDisk 128GB Extreme PRO SDXC UHS-I Memory Card - C10, U3, V30, 4K UHD, SD Card - SDSDXXD-128G-GN4IN
Best Seller

SanDisk 128GB Extreme PRO SDXC UHS-I Memory Card - C10, U3, V30, 4K UHD, SD Card - SDSDXXD-128G-GN4IN

(71,074)
$29.99$26.61
FREE delivery Sun, Aug 10 on your first order
4K+ bought in past month
Loading...
Lexar 128GB Micro SD Card, microSDXC UHS-I Flash Memory Card with Adapter - Up to 100MB/s, A1, U3, Class10, V30, High Speed TF Card

Lexar 128GB Micro SD Card, microSDXC UHS-I Flash Memory Card with Adapter - Up to 100MB/s, A1, U3, Class10, V30, High Speed TF Card

(26,884)
$18.99
FREE delivery Wed, Aug 20 on your first order
5K+ bought in past month
Loading...
ANXRE 71" Phone Tripod, NT79 Extendable Selfie Stick for Cell PhoneοΌ†Camera Photo Video Kits, Travel Tripod Stand with Remote for Video Recording, Phone Holder Tripod Compatible with Phone Camera Gopro

ANXRE 71" Phone Tripod, NT79 Extendable Selfie Stick for Cell PhoneοΌ†Camera Photo Video Kits, Travel Tripod Stand with Remote for Video Recording, Phone Holder Tripod Compatible with Phone Camera Gopro

(752)
$23.99
FREE delivery Tue, Aug 19 on your first order
2K+ bought in past month
Loading...
Kaiess 62" Tripod for iPhone, Selfie Stick Tripod & Phone Tripod Stand with Remote, Cell Phone Tripod for iPhone, Extendable Travel Tripod Compatible with iPhone 14/13/12 Pro Max/Android
Best Seller

Kaiess 62" Tripod for iPhone, Selfie Stick Tripod & Phone Tripod Stand with Remote, Cell Phone Tripod for iPhone, Extendable Travel Tripod Compatible with iPhone 14/13/12 Pro Max/Android

(5,298)
$28.98
FREE delivery Sat, Jun 14 on your first order
1K+ bought in past month
Loading...
EUCOS 62" Phone Tripod, Tripod for iPhone & Selfie Stick Tripod with Remote, Extendable Phone Tripod Stand & Travel Tripod, Solidest Cell Phone Tripod Compatible with iPhone/Android

EUCOS 62" Phone Tripod, Tripod for iPhone & Selfie Stick Tripod with Remote, Extendable Phone Tripod Stand & Travel Tripod, Solidest Cell Phone Tripod Compatible with iPhone/Android

(13,783)
$28.99
Prime
5K+ bought in past month
Loading...
Jhcztrk 17.3 Inch Portable Monitor HDMI Type-C Laptop Gaming Monitor, 1600X900 with Built-in Speakers and Tripod External Slim Travel Monitors Second Monitors for Mac Smartphone PS4/PS5/ Gaming

Jhcztrk 17.3 Inch Portable Monitor HDMI Type-C Laptop Gaming Monitor, 1600X900 with Built-in Speakers and Tripod External Slim Travel Monitors Second Monitors for Mac Smartphone PS4/PS5/ Gaming

(32)
$161.69$161.68
FREE delivery Aug 11 - 13