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

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ม.ค. 2025

ความคิดเห็น • 25

  • @murali1790able
    @murali1790able 3 ปีที่แล้ว

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

  • @rajatbudania6181
    @rajatbudania6181 4 ปีที่แล้ว +2

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

    • @CodingSimplified
      @CodingSimplified  4 ปีที่แล้ว

      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.

  • @sameer1571
    @sameer1571 4 ปีที่แล้ว +2

    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

    • @sameer1571
      @sameer1571 4 ปีที่แล้ว

      And 1 more thing I think there can be integer overflow also when you are adding integer. Maxvalue with something

  • @kumarakash5219
    @kumarakash5219 3 ปีที่แล้ว

    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

  • @pqazx1
    @pqazx1 4 ปีที่แล้ว +1

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

    • @CodingSimplified
      @CodingSimplified  4 ปีที่แล้ว +1

      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
      @pqazx1 4 ปีที่แล้ว

      Why are we not checking for right value even if we havegot a value from the left side??

    • @CodingSimplified
      @CodingSimplified  4 ปีที่แล้ว

      @@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. )

  • @ayushupadhyaya739
    @ayushupadhyaya739 4 ปีที่แล้ว

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

    • @CodingSimplified
      @CodingSimplified  4 ปีที่แล้ว

      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.

  • @aayush5474
    @aayush5474 4 ปีที่แล้ว +1

    my mind has been blown!

    • @CodingSimplified
      @CodingSimplified  4 ปีที่แล้ว

      Thanks for your nice feedback. Keep Watching.

    • @aayush5474
      @aayush5474 4 ปีที่แล้ว

      @@CodingSimplified bro how to print the leaf node as well

  • @AyushiSharmaDSA
    @AyushiSharmaDSA 4 ปีที่แล้ว +1

    Awesome explanation..Thanks for making this video :)

    • @CodingSimplified
      @CodingSimplified  4 ปีที่แล้ว +1

      Thanks Ayushi. Keep Watching our other videos as well.

  • @rongav1997
    @rongav1997 4 ปีที่แล้ว +2

    This was a really solid video thank you for this!

  • @asthakur14
    @asthakur14 4 ปีที่แล้ว +1

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

    • @CodingSimplified
      @CodingSimplified  4 ปีที่แล้ว

      Thanks for your nice feedback. Keep Watching.

  • @chodingninjas7415
    @chodingninjas7415 5 ปีที่แล้ว

    can u do serialization ans deserialization

    • @CodingSimplified
      @CodingSimplified  5 ปีที่แล้ว +1

      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.

    • @chodingninjas7415
      @chodingninjas7415 5 ปีที่แล้ว

      Great

    • @CodingSimplified
      @CodingSimplified  5 ปีที่แล้ว +2

      As you requested, we've launched video about it: th-cam.com/video/lUnNK9jPg2Y/w-d-xo.html