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
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.
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
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.
That's like saying if you have 0 dollars in your bank account you're 1 dollar in debt. A 0 node tree has height 0 in the same way 0 dollars = 0 dollars
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.
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.
@@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
@@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
@@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
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
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.
Also works with k-ary trees, instead of calling the function on tree.left and tree.right, you call the function on each children by using a for loop then you join the results (Example: sum of nodes of a k-ary tree: ... sum_children = 0 for children in tree.children: sum_children += tree_sum(children) return tree.val + sum_children
This is the most undervalued tree algorithm video.
Info here is gold. Thanks man!
Thanks a lot for your comment
Dude what an explanation. Thank you so much, using recursion to solve binary tree problems has been a really huge problem till now
I love people who make things this simple, just subscribed to your channel.
Thanks a lot!
Wow. Brilliant nugget!! Literally opened my eyes to the problems I am solving!!
same😍😍
This is actually an amazing piece of intuition
Discover the new graph theory algorithms course: inscod.com/graphalgo
🔴
/ \
🔵-🔴
| |
🔴-🔵
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
recursion is really messing with my mind
true af
Trueeeee😭😭
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.
Thanks!!
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.
Yes it doesn't apply for all types of trees sure
@@insidecode do u have a similar video coming for binary search trees
Very easy described man!
Give this man a medal!
Thankss ❤️
For height of tree it must return -1 if root is null.(2.42)
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
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
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!
Fix: At 2:38, the base case should return -1 not 0
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.
@@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
That's like saying if you have 0 dollars in your bank account you're 1 dollar in debt. A 0 node tree has height 0 in the same way 0 dollars = 0 dollars
Bruh. This finally makes sense.
This was the coolest explanation of complex problem ever..Thank you
You're welcome!
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.
Honestly....
This video is just the best!!!
You just earned a new subscriber....
Spent hours searching for a simple explanation on Trees, found it in your channel.
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.
Finally, this is what I have been looking for; algorithms with proper visualizations. I had to buy all your 5 courses on udemy
Thanks a lot for your comment!
hello, is his dynamic programming course in python?
Hello, yes it is
@@insidecode thanks for the reply, I will purchase it. are all your other courses on Udemy in python as well ?
@@ifeanyi3713 You're welcome! Yes
bro just opened my mind
u r genius...the most simple ways i see to learn this bianry tree...tysm blud
Amazing way of presenting… it’s awesome
amazing video !! please make more videos in binary trees and graphs
Okay!
MAN YOU MADE ME UNDERSTAND RECURSION, finally! Thank you so much!
You're welcome! Check the 3 hours recursion course I published recently here
@@insidecode I’ll check it out. Thank you!
@@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
@@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
@@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
thank you for this video. i'm really bad at binary trees but this way of thinking about it really helps
LEGIT MAGICK!!!!! i just solved a question using this logic. SO LUCID. SO CLEAR. TO THE POINT
To the point and well explained!
The sum of elements at 1:21 is not 120.
@@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.
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
This is exactly what I was looking for💥
This man is literally the best!!!
Thanks a lot!
Thanks man, this is awesome!!
Super helpful ! Great content
Thanks
Thanks we got a whole diffrent persepective of solving problem. Could you make this kinda videos for other DS also, pls
You're welcome! I'll see
Wow, thats such a good approach! Thanks
You're welcome!
Very sell described. Good work. Keep it up👍
Thanks!
wow this actually helped me so much thx
Remember guys for understanding recursion you first need to understand recursion
Superb explanation , Thanks.
Just the best idea to approach binary 🌲 questions
Thanku sor finally i understood Tree data after 2 months ,thanks a lot Your content is freat
You're welcome!
Amazing man! very helpful
This actually works holy shit this is insane
Thank you so much for this video!
Glad it was helpful!
Can we solve every binary tree problem using either BFS or DFS?
Great video! I'm definetly subscribing😄
Thanks for subbing!
great explanation sir, thank you so much
Recursion is the base algo you need to master before learning trees, If you are good at recursion then trees is nothing.
Such a good video! But we have to think that recursion is not always the best solution
what is the complexity of this kind of algorithms?
1:20 the sum will be 110 instead of 120
You took time to calculate :0 But thanks for telling me!
wow very well explained, thank you sooo much!
you're welcome!
Help full video thank you❤
Your code in 1:31 doesn’t seem to work
What error you got? Maybe you didn't create Tree class or something
Will this formula work for Visible Tree Node problem? If yes, how? Thank you!
Kindly make a similiar video on Graphs. It will be soo helpfull. Thanks
the best video I have ever seen in my life definitely a like. and a subscribe
This is super cool. Thanks 👍🏻
You're welcome
Isn't this just post-order DFS?
Super helpful bro. Thanks a lot ❣️
You're welcome!
Wonderful explaination
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.
isn't the tree that appears at 30 seconds not a valid bst?
It's not supposed to be a BST
Wow! Super helpful!
simple beneficial and inspiring
SUPER HELPFUL!
Omg it's literally always the same thing, how have I never noticed this before?
It's really Awesome 🤩
You've earned a new subscriber 🔥
Thankss!
How do you create the animation? Are you using any software for tha?
With PowerPoint
@@insidecodeOhhk. Thanks a lot.
@@shashicsnitjsr You're welcome!
best video. perfectly explained.
Thanks!
This nails it
Hi..i have to appear a HireVue interview soon for Deloitte. Will this coursepack be useful to me?
It contains a lot of popular problems on different patterns and data structures, so it will be useful for you for sure
that bri-eff though XD (brief is said as breef, not bri-eff)
oh thanks for info!
Wow 😎 🥺 thanks brother no more grinding
OMG thank u so much !
But what about trees that are not binary?
Also works with k-ary trees, instead of calling the function on tree.left and tree.right, you call the function on each children by using a for loop then you join the results (Example: sum of nodes of a k-ary tree:
...
sum_children = 0
for children in tree.children:
sum_children += tree_sum(children)
return tree.val + sum_children
@@insidecode Thanks!
@@yoman9446 You're welcome!
A BIG Thank you 🌹
You're welcome!
Damn never knew tree's can be broken down like this
How do you learn this for sure?
Learn what?
Can we have pdf of these codes?
I didn't make a PDF of them sorry
This is amazing
Very Nice.God bless you..
Thanks a lot🖤
You're welcome!
OMg this is amazing lol thank you so much
You're welcome!
can I donate to you on patreon? I rly liked this video.
Why you are so smart?
haha 😂
Great video
Thanks, share code too!!
Great channel
thank you!
You're welcome!
Please dont try to eat mic when ur speaking. Its not a proper etiquette. Btw very nice explanation.
Thanks! This is quite an old video, can you check latest ones and tell me if sound quality improved?
@@insidecode Woah its really good. Just curious what mic do you use now?
The same mic (M-Audio Uber mic), just improved audio post processing
Underated video
Watch it in 0.75x
4:20 nice
Recursive thinking
awesome, thxxxxx
Thanks
Thx a lot!!!!!
You're welcome!
Easy and straight forward