How to solve (almost) any binary tree coding problem

  Рет қаралды 156,565

Inside code

Inside code

3 жыл бұрын

🔴 Learn graph theory algorithms: inscod.com/graphalgo
⚙ Learn dynamic programming: inscod.com/dp_course
💡 Learn to solve popular coding interview problems: inscod.com/50problems_course
⌛ Learn time and space complexity analysis: inscod.com/complexity_course
🔁 Learn recursion: inscod.com/recursion_course
NB: This video is ad-free, you can choose to support Inside code by purchasing one of the courses above or dropping a super thanks!
NB2: Discounts of courses above are permanent
I also post content on LinkedIn (inscod.com/linkedin) and Instagram (inscod.com/instagram)

Пікірлер: 189
@edy231094
@edy231094 3 жыл бұрын
This is the most undervalued tree algorithm video. Info here is gold. Thanks man!
@insidecode
@insidecode 3 жыл бұрын
Thanks a lot for your comment
@tahaather7429
@tahaather7429 4 ай бұрын
Dude what an explanation. Thank you so much, using recursion to solve binary tree problems has been a really huge problem till now
@cakec9
@cakec9 3 жыл бұрын
Wow. Brilliant nugget!! Literally opened my eyes to the problems I am solving!!
@twi4458
@twi4458 2 жыл бұрын
same😍😍
@kwamenani7775
@kwamenani7775 2 жыл бұрын
I love people who make things this simple, just subscribed to your channel.
@insidecode
@insidecode 2 жыл бұрын
Thanks a lot!
@iez
@iez 2 жыл бұрын
This is actually an amazing piece of intuition
@karthigeyanramesh4477
@karthigeyanramesh4477 6 ай бұрын
I just wanna say, that thinking of recursion in a way where we fragment the root node made sooooo much sense. My exam is next week, thanks!
@sergeynazarov2473
@sergeynazarov2473 2 ай бұрын
With all respect to the author, this is trivial problems, I thought you clarify at least one of non-trivial: controlling levels of tree, controlling branches of tree, controlling nodes of tree or controlling symmethric of tree.
@auntyBill
@auntyBill 23 күн бұрын
Amazing way of presenting… it’s awesome
@sonijain4399
@sonijain4399 11 ай бұрын
Thank u so so soo much man Non linear data structures were really tough for me From school to college It was tough for me to understand But now when i am preparing for a job I saw your video And u saved my life Thank u so much
@weixiangwong2227
@weixiangwong2227 Жыл бұрын
thank you for this video. i'm really bad at binary trees but this way of thinking about it really helps
@monarch2397
@monarch2397 5 ай бұрын
u r genius...the most simple ways i see to learn this bianry tree...tysm blud
@pyrostation678
@pyrostation678 2 жыл бұрын
Thanks man, this is awesome!!
@indraneel6601
@indraneel6601 3 жыл бұрын
Give this guy a whole lot of subscribers!!!!!! I feel confident to solve tree problems.You Rock man Please Create a video how to solve graph problems.
@insidecode
@insidecode 3 жыл бұрын
Thanks!!
@levidworkin851
@levidworkin851 2 ай бұрын
Hey Inside Code I loved the video! I just wanted to correct the treeHeight algorithm. It needs to return -1 if the root is None because currently with the returning of 0, the leaf nodes get a height of 1 instead of a height of 0 which will cause the resulting tree height to be 1 greater than it should be.
@chandandigumarthy9652
@chandandigumarthy9652 2 жыл бұрын
Amazing man! very helpful
@vyankateshkulkarni4374
@vyankateshkulkarni4374 2 жыл бұрын
great explanation sir, thank you so much
@RR-wx8gp
@RR-wx8gp Ай бұрын
Honestly.... This video is just the best!!!
@KGTube
@KGTube 3 жыл бұрын
Very easy described man! Give this man a medal!
@insidecode
@insidecode 3 жыл бұрын
Thankss ❤️
@ragapriyakarthikeyan3139
@ragapriyakarthikeyan3139 Ай бұрын
This is exactly what I was looking for💥
@TomGoo
@TomGoo 3 ай бұрын
wow this actually helped me so much thx
@shivansh2581
@shivansh2581 Жыл бұрын
LEGIT MAGICK!!!!! i just solved a question using this logic. SO LUCID. SO CLEAR. TO THE POINT
@marcin-guitar
@marcin-guitar 2 жыл бұрын
Well, this is true for a certain kind of trees, where each node counts as equivalent. The same algorithm doesn't necessarily apply to segment trees, binary search trees, KD-trees etc. where an element's position in the tree is more important than its sole presence in the tree.
@insidecode
@insidecode 2 жыл бұрын
Yes it doesn't apply for all types of trees sure
@coolkid9206
@coolkid9206 Жыл бұрын
@@insidecode do u have a similar video coming for binary search trees
@isaacfs8995
@isaacfs8995 3 жыл бұрын
SUPER HELPFUL!
@Satyamkumar19113
@Satyamkumar19113 Ай бұрын
Just the best idea to approach binary 🌲 questions
@igbalajobizhikrullah465
@igbalajobizhikrullah465 2 жыл бұрын
You just earned a new subscriber.... Spent hours searching for a simple explanation on Trees, found it in your channel.
@insidecode
@insidecode 2 жыл бұрын
@procode6881
@procode6881 3 жыл бұрын
Wonderful explaination
@astronautonmars
@astronautonmars 2 жыл бұрын
Wow! Super helpful!
@harinijeyaraman8789
@harinijeyaraman8789 3 жыл бұрын
Super helpful ! Great content
@insidecode
@insidecode 3 жыл бұрын
Thanks
@prempeacefulchannel
@prempeacefulchannel 2 жыл бұрын
Wow, thats such a good approach! Thanks
@insidecode
@insidecode 2 жыл бұрын
You're welcome!
@fifibishi7786
@fifibishi7786 3 жыл бұрын
wow very well explained, thank you sooo much!
@insidecode
@insidecode 3 жыл бұрын
you're welcome!
@mdzikrullah9575
@mdzikrullah9575 3 жыл бұрын
This was the coolest explanation of complex problem ever..Thank you
@insidecode
@insidecode 3 жыл бұрын
You're welcome!
@DaggerMan11
@DaggerMan11 3 жыл бұрын
exactly. Once you realize these complex problems all have some pretty easy-to-remember patterns that make them easier to solve, it's just a matter of knowing which pattern to apply.
@vijayola7597
@vijayola7597 2 жыл бұрын
amazing video !! please make more videos in binary trees and graphs
@insidecode
@insidecode 2 жыл бұрын
Okay!
@StrangeDooropen
@StrangeDooropen 5 ай бұрын
OMG thank u so much !
@curesnow6493
@curesnow6493 Жыл бұрын
Thank you so much for this video!
@insidecode
@insidecode Жыл бұрын
Glad it was helpful!
@mdzikrullah9575
@mdzikrullah9575 2 жыл бұрын
Kindly make a similiar video on Graphs. It will be soo helpfull. Thanks
@iezioaudi22
@iezioaudi22 3 ай бұрын
THANK YOU!!
@paulamorillasalonso3076
@paulamorillasalonso3076 2 жыл бұрын
Great video! I'm definetly subscribing😄
@insidecode
@insidecode 2 жыл бұрын
Thanks for subbing!
@JohnDoe-yb9rg
@JohnDoe-yb9rg 2 жыл бұрын
Will this formula work for Visible Tree Node problem? If yes, how? Thank you!
@hangoutwithabhi
@hangoutwithabhi 3 жыл бұрын
This is super cool. Thanks 👍🏻
@insidecode
@insidecode 3 жыл бұрын
You're welcome
@dhairya18
@dhairya18 3 жыл бұрын
Very sell described. Good work. Keep it up👍
@insidecode
@insidecode 3 жыл бұрын
Thanks!
@paladin5d781
@paladin5d781 Жыл бұрын
simple beneficial and inspiring
@twi4458
@twi4458 2 жыл бұрын
Thanks we got a whole diffrent persepective of solving problem. Could you make this kinda videos for other DS also, pls
@insidecode
@insidecode 2 жыл бұрын
You're welcome! I'll see
@hydromarlon4168
@hydromarlon4168 2 жыл бұрын
Bruh. This finally makes sense.
@decayingskulls7088
@decayingskulls7088 2 жыл бұрын
bro just opened my mind
@rishav2520
@rishav2520 3 жыл бұрын
It's really Awesome 🤩
@danielapineyro1998
@danielapineyro1998 3 жыл бұрын
MAN YOU MADE ME UNDERSTAND RECURSION, finally! Thank you so much!
@insidecode
@insidecode 3 жыл бұрын
You're welcome! Check the 3 hours recursion course I published recently here
@danielapineyro1998
@danielapineyro1998 3 жыл бұрын
@@insidecode I’ll check it out. Thank you!
@insidecode
@insidecode 3 жыл бұрын
@@danielapineyro1998 I don't have a video on that but I can explain it to you here, did you see in this video the function where we calculate the sum? We calculate the sum from the left, the sum from the right, and we add to them the parent's value. For your problem, you take the same function, but you just add two things, a global boolean variable output that starts at true, and a condition before returning the sum in the recursive function, that condition is: if root.data != left+right: output = False
@insidecode
@insidecode 3 жыл бұрын
@@danielapineyro1998 What we're doing here is that we're traversing the tree, but at each node, after calculating the sum of both subtrees, we're comparing with the parent's value, and we're setting the final output to false if they're different, we used a global variable to be able to access it from any call
@insidecode
@insidecode 3 жыл бұрын
@@danielapineyro1998 The code would be something like this: output = True def tree_sum(root): global output if root is None: return 0 else: left = tree_sum(root.left) right = tree_sum(root.right) if root.data != left+right: output = False return root.data + left + right def all_equal(root): global output tree_sum(output) return output
@lukehatcher98
@lukehatcher98 3 жыл бұрын
Great video
@filmbotreviews
@filmbotreviews Жыл бұрын
High quality video! Thanks
@insidecode
@insidecode Жыл бұрын
You're welcome
@kirillzlobin7135
@kirillzlobin7135 7 ай бұрын
Great channel
@tanujm87
@tanujm87 3 жыл бұрын
Super helpful bro. Thanks a lot ❣️
@insidecode
@insidecode 3 жыл бұрын
You're welcome!
@amanr007
@amanr007 2 жыл бұрын
Wow 😎 🥺 thanks brother no more grinding
@longchikanouo4905
@longchikanouo4905 2 жыл бұрын
Finally, this is what I have been looking for; algorithms with proper visualizations. I had to buy all your 5 courses on udemy
@insidecode
@insidecode 2 жыл бұрын
Thanks a lot for your comment!
@ifeanyi3713
@ifeanyi3713 2 жыл бұрын
hello, is his dynamic programming course in python?
@insidecode
@insidecode 2 жыл бұрын
Hello, yes it is
@ifeanyi3713
@ifeanyi3713 2 жыл бұрын
@@insidecode thanks for the reply, I will purchase it. are all your other courses on Udemy in python as well ?
@insidecode
@insidecode 2 жыл бұрын
@@ifeanyi3713 You're welcome! Yes
@chetanshrivastava3762
@chetanshrivastava3762 3 жыл бұрын
Very Nice.God bless you..
@shechan456
@shechan456 2 жыл бұрын
Thanku sor finally i understood Tree data after 2 months ,thanks a lot Your content is freat
@insidecode
@insidecode 2 жыл бұрын
You're welcome!
@BryceChudomelka
@BryceChudomelka 2 жыл бұрын
This is amazing
@subee128
@subee128 Ай бұрын
Thanks
@josiahdavid7735
@josiahdavid7735 2 жыл бұрын
This man is literally the best!!!
@insidecode
@insidecode 2 жыл бұрын
Thanks a lot!
@mayankkhanna9644
@mayankkhanna9644 2 жыл бұрын
Can we solve every binary tree problem using either BFS or DFS?
@pranjalmishra2602
@pranjalmishra2602 3 жыл бұрын
Amazing
@Radi0he4d1
@Radi0he4d1 2 жыл бұрын
This nails it
@fahmidurarnob2470
@fahmidurarnob2470 2 жыл бұрын
Thanks a lot🖤
@insidecode
@insidecode 2 жыл бұрын
You're welcome!
@myrelaxpoint
@myrelaxpoint 3 жыл бұрын
A BIG Thank you 🌹
@insidecode
@insidecode 3 жыл бұрын
You're welcome!
@coolkid9206
@coolkid9206 Жыл бұрын
This actually works holy shit this is insane
@ojas2121
@ojas2121 3 жыл бұрын
best video. perfectly explained.
@insidecode
@insidecode 3 жыл бұрын
Thanks!
@gurudevsanthosh8483
@gurudevsanthosh8483 Жыл бұрын
For height of tree it must return -1 if root is null.(2.42)
@siddharthvikram3054
@siddharthvikram3054 Жыл бұрын
that depends if u are taking the height of a leaf node to be 1 or 0, it depends upon the book u are using
@mahmudulahsan
@mahmudulahsan 2 жыл бұрын
Great!
@khanmuhammad3021
@khanmuhammad3021 2 жыл бұрын
Wow amazing
@markfung1
@markfung1 2 жыл бұрын
OMg this is amazing lol thank you so much
@insidecode
@insidecode 2 жыл бұрын
You're welcome!
@hildamartin8022
@hildamartin8022 2 жыл бұрын
thank you!
@insidecode
@insidecode 2 жыл бұрын
You're welcome!
@minusCEE
@minusCEE 2 жыл бұрын
Underated video
@harsh9558
@harsh9558 2 жыл бұрын
Thx a lot!!!!!
@insidecode
@insidecode 2 жыл бұрын
You're welcome!
@niketkumardheeryan3865
@niketkumardheeryan3865 3 жыл бұрын
Thank u
@istiyak_8426
@istiyak_8426 3 жыл бұрын
Sukriya Sir
@lorenzo.fiorini
@lorenzo.fiorini 3 жыл бұрын
Easy and straight forward
@adithiyagurupatham3255
@adithiyagurupatham3255 2 жыл бұрын
You've earned a new subscriber 🔥
@insidecode
@insidecode 2 жыл бұрын
Thankss!
@alexirabor
@alexirabor 2 жыл бұрын
Thank you
@insidecode
@insidecode 2 жыл бұрын
Welcome!
@eshank2508
@eshank2508 3 жыл бұрын
Hi..i have to appear a HireVue interview soon for Deloitte. Will this coursepack be useful to me?
@insidecode
@insidecode 3 жыл бұрын
It contains a lot of popular problems on different patterns and data structures, so it will be useful for you for sure
@shashicsnitjsr
@shashicsnitjsr 2 жыл бұрын
How do you create the animation? Are you using any software for tha?
@insidecode
@insidecode 2 жыл бұрын
With PowerPoint
@shashicsnitjsr
@shashicsnitjsr 2 жыл бұрын
@@insidecodeOhhk. Thanks a lot.
@insidecode
@insidecode 2 жыл бұрын
@@shashicsnitjsr You're welcome!
@vikasagrawal9945
@vikasagrawal9945 Жыл бұрын
execellent
@AviPars
@AviPars 2 жыл бұрын
Thanks, share code too!!
@samxsharan
@samxsharan 3 жыл бұрын
@2:40 will max( leftHeight , rightHeight ) return --> ' 1 ' ?
@insidecode
@insidecode 3 жыл бұрын
It depends on where we are in the tree
@samxsharan
@samxsharan 3 жыл бұрын
@@insidecode gives '1' till it meets base condition?
@insidecode
@insidecode 3 жыл бұрын
No it can give more than 1, try to apply the algorithm on a tree and you'll see
@samxsharan
@samxsharan 3 жыл бұрын
@@insidecode gotcha🙌🏾
@jacobmoore8734
@jacobmoore8734 Жыл бұрын
Omg it's literally always the same thing, how have I never noticed this before?
@d3v-x64
@d3v-x64 2 жыл бұрын
omg thank you, you saved my ass
@PepeTostado
@PepeTostado 2 жыл бұрын
How do you learn this for sure?
@insidecode
@insidecode 2 жыл бұрын
Learn what?
@iangoodman7273
@iangoodman7273 2 жыл бұрын
isn't the tree that appears at 30 seconds not a valid bst?
@insidecode
@insidecode 2 жыл бұрын
It's not supposed to be a BST
@sniff4643
@sniff4643 3 жыл бұрын
can I donate to you on patreon? I rly liked this video.
@insidecode
@insidecode Жыл бұрын
Discover the new graph theory algorithms course: inscod.com/graphalgo 🔴 / \ 🔵-🔴 | | 🔴-🔵
@emiledin2183
@emiledin2183 Жыл бұрын
I will leave a dislike because you didn't explain how it actually works in the code. Like at 1:41 what does assigning a variable to the recursive function even do and mean? Please help me understand. Like why assign the variable and not just return root.data + treesum(root.left) + treesum(root.right) instead? Thank you
@human-011
@human-011 3 жыл бұрын
awesome x 100
@santoshreddy9963
@santoshreddy9963 3 жыл бұрын
Oooooo maa gooooo, turuuuuu triiick
@KeshavKumar-qz7ow
@KeshavKumar-qz7ow 3 жыл бұрын
1:20 the sum will be 110 instead of 120
@insidecode
@insidecode 3 жыл бұрын
You took time to calculate :0 But thanks for telling me!
@manikantpandey4753
@manikantpandey4753 2 жыл бұрын
Can we have pdf of these codes?
@insidecode
@insidecode 2 жыл бұрын
I didn't make a PDF of them sorry
@rajathrao3209
@rajathrao3209 2 жыл бұрын
Damn never knew tree's can be broken down like this
@oladipotimothy6007
@oladipotimothy6007 2 жыл бұрын
Recursive thinking
@MrRyanroberson1
@MrRyanroberson1 2 жыл бұрын
that bri-eff though XD (brief is said as breef, not bri-eff)
@insidecode
@insidecode 2 жыл бұрын
oh thanks for info!
@servantofthelord8147
@servantofthelord8147 Жыл бұрын
Wow.
@rclarke88
@rclarke88 3 жыл бұрын
The sum of elements at 1:21 is not 120.
@rclarke88
@rclarke88 3 жыл бұрын
@@insidecode That was a bit nit-picky of me to point that out. The video was still very good and I look forward to watching more from you.
@insidecode
@insidecode Жыл бұрын
Hello, after watching it again, it's actually 120, because we're returning sum of left subtree + sum of right subtree + root's value, and 52 + 53 + 5 gives 120 not 115, you forgot to also add the root's value which is 5
@cocoarecords
@cocoarecords 3 жыл бұрын
👍
@droid806
@droid806 3 жыл бұрын
Your code in 1:31 doesn’t seem to work
@insidecode
@insidecode 3 жыл бұрын
What error you got? Maybe you didn't create Tree class or something
@insidecode
@insidecode 3 жыл бұрын
Fix: At 2:38, the base case should return -1 not 0
@Raven-zk2xq
@Raven-zk2xq 3 жыл бұрын
Maybe I misunderstand why, but I think of it like this. If the input is simple None to begin with, is the height 0 or is it 1? From my understanding it would be 0, similar to how an len(arr) where arr is an empty array would also return 0.
@insidecode
@insidecode 3 жыл бұрын
@@Raven-zk2xq Yes but height 0 is the height of a tree with one node only (the root), so when a tree has no nodes at all, its height is -1
@prapanchayatrikudu6437
@prapanchayatrikudu6437 3 жыл бұрын
Excellemt
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
Would you like a delicious big mooncake? #shorts#Mooncake #China #Chinesefood
00:30
Joven bailarín noquea a ladrón de un golpe #nmas #shorts
00:17
Тяжелые будни жены
00:46
К-Media
Рет қаралды 5 МЛН
Dynamic #gadgets for math genius! #maths
00:29
FLIP FLOP Hacks
Рет қаралды 18 МЛН
Whiteboard Coding Interviews: 6 Steps to Solve Any Problem
15:18
Fullstack Academy
Рет қаралды 357 М.
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,2 МЛН
Data Structures: Trees
9:57
HackerRank
Рет қаралды 994 М.
How to solve binary tree path-related problems - Inside code
5:32
How I Got Good at Coding Interviews
6:29
NeetCode
Рет қаралды 1,6 МЛН
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 589 М.
Learn Binary search trees in 20 minutes 🔍
20:25
Bro Code
Рет қаралды 136 М.
Introduction to Recursion - Learn In The Best Way
1:55:49
Kunal Kushwaha
Рет қаралды 816 М.
Heaps, heapsort, and priority queues - Inside code
19:01
Inside code
Рет қаралды 68 М.
Дени против умной колонки😁
0:40
Deni & Mani
Рет қаралды 9 МЛН
XL-Power Best For Audio Call 📞 Mobile 📱
0:42
Tech Official
Рет қаралды 773 М.
Pratik Cat6 kablo soyma
0:15
Elektrik-Elektronik
Рет қаралды 8 МЛН