Binary tree - 69: Get closest leaf node distance from a given value in Binary Tree

preview_player
Показать описание
Solution:
- We'll traverse the Binary tree in preorder manner
- while traversing we'll store the node reference in node array
- If node value matches to given value, we'll find min distance to leaf from this value, as well we'll get minimum distance to leaf node from it's parent as well

- Time Complexity: O(n) * Height of Tree
- Space Complexity: O(Height Of Tree)

Do Watch video for more info

This Problem is synonym of following problems:
binary tree Get closest leaf node distance from a given value,
Get closest leaf node distance from a given value in Binary Tree,
coding simplified,

CHECK OUT CODING SIMPLIFIED

★☆★ VIEW THE BLOG POST: ★☆★

I started my YouTube channel, Coding Simplified, during Dec of 2015.
Since then, I've published over 200+ videos.

★☆★ SUBSCRIBE TO ME ON YOUTUBE: ★☆★

★☆★ Send us mail at: ★☆★
Рекомендации по теме
Комментарии
Автор

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

sameer
Автор

This was a really solid video thank you for this!

rongav
Автор

Awesome explanation..Thanks for making this video :)

AyushiSharmaDSA
Автор

damn you made it seem easy..it was very helpful Thank you for this!

asthakur
Автор

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 ??

rajatbudania
Автор

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

kumarakash
Автор

20:18 line 39 and 45 Math.Min is unnecessary

muraliable
Автор

i was thinking to run bfs from target node and the first leaf we get is the minimum distance from the target node.

ayushupadhyaya
Автор

How do we know if left returns the ans then no need to go in the right side??what is the logic here??

pqazx
Автор

can u do serialization ans deserialization

chodingninjas
visit shbcf.ru