Can we do it this way. 1) first traverse the tree and store the parent of every node in a map, so that now we have left right and the parent , ie. It has sort of converted in graph. 2) Simply apply bfs from the required node and return level whenever we encounter a leaf node for the first time. Plz twll whether this approach will work fine ??
Even if we store the parent of each node, once you find required node, you need to see closed lead from all it's parent. So via parent, again you need to go to leaf node. This sums up to same approach that we're following in video. - So yes, storing parent will definitely work, just that we need to traverse to leaf node as well.
No doubt why you are getting so much love hats off to the way you explained it And one more thing can we use map to keep the track of the distance of closest leaf from given val like for 6 we had already calculated the closest leaf now when 7 call 6 it will do the same work again similary when 2 will call 7 again the same work
As we can lca from that node to leaf node and than finding the total no .of node from that ancestor and one maximum variable to have the count of maximum
At 20.08, we've explained it: if(minDistFromLeaf != Integer.MAX_VALUE) { return minDistFromLeaf; } Above code will make sure, if we've got value from left, then no need to go to right side. Thanks.
@@pqazx1 Because once we find our target node in left side, we solve it there & get value, so there's no point to check in right side. From right we'll only get MAX value as value exists only in left. (Target node exists only at 1 place. )
Via this, you'll cater to only those nodes, which are originating from targetNode. But closest leaf can be present in subtree of any parent of targetNode. Please see example mentioned in video, then you'll get some more info. Hope it helps you. Thanks.
20:18 line 39 and 45 Math.Min is unnecessary
Can we do it this way.
1) first traverse the tree and store the parent of every node in a map, so that now we have left right and the parent , ie. It has sort of converted in graph.
2) Simply apply bfs from the required node and return level whenever we encounter a leaf node for the first time.
Plz twll whether this approach will work fine ??
Even if we store the parent of each node, once you find required node, you need to see closed lead from all it's parent. So via parent, again you need to go to leaf node. This sums up to same approach that we're following in video.
- So yes, storing parent will definitely work, just that we need to traverse to leaf node as well.
No doubt why you are getting so much love hats off to the way you explained it
And one more thing can we use map to keep the track of the distance of closest leaf from given val like for 6 we had already calculated the closest leaf now when 7 call 6 it will do the same work again similary when 2 will call 7 again the same work
And 1 more thing I think there can be integer overflow also when you are adding integer. Maxvalue with something
As we can lca from that node to leaf node and than finding the total no .of node from that ancestor and one maximum variable to have the count of maximum
How do we know if left returns the ans then no need to go in the right side??what is the logic here??
At 20.08, we've explained it:
if(minDistFromLeaf != Integer.MAX_VALUE) {
return minDistFromLeaf;
}
Above code will make sure, if we've got value from left, then no need to go to right side. Thanks.
Why are we not checking for right value even if we havegot a value from the left side??
@@pqazx1 Because once we find our target node in left side, we solve it there & get value, so there's no point to check in right side. From right we'll only get MAX value as value exists only in left. (Target node exists only at 1 place. )
i was thinking to run bfs from target node and the first leaf we get is the minimum distance from the target node.
Via this, you'll cater to only those nodes, which are originating from targetNode. But closest leaf can be present in subtree of any parent of targetNode. Please see example mentioned in video, then you'll get some more info.
Hope it helps you. Thanks.
my mind has been blown!
Thanks for your nice feedback. Keep Watching.
@@CodingSimplified bro how to print the leaf node as well
Awesome explanation..Thanks for making this video :)
Thanks Ayushi. Keep Watching our other videos as well.
This was a really solid video thank you for this!
Thanks.
damn you made it seem easy..it was very helpful Thank you for this!
Thanks for your nice feedback. Keep Watching.
can u do serialization ans deserialization
Sure, this is in my list. In 2-3 days, I'll upload it. If you've already subscribed, then you'll be notified about it.
Great
As you requested, we've launched video about it: th-cam.com/video/lUnNK9jPg2Y/w-d-xo.html