Trim a Binary Search Tree - Leetcode 669 - Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 พ.ย. 2024

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

  • @freddy5638
    @freddy5638 3 ปีที่แล้ว +21

    It’s always a good day when Neet uploads

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

    Here's how I solved it. I did it similar to how you did "Flatten Binary Tree". It's similar because this way we solve each subtree in post order DFS and then decide which nodes to return based on the conditions (build it from ground up). The solution in this video is a little too elegant and hard to understand
    var trimBST = function(root, low, high) {
    if (root == null) {
    return null
    }
    let left = trimBST(root.left, low, high)
    let right = trimBST(root.right, low, high)
    root.left = left
    root.right = right
    if (root.val < low || root.val > high) {
    // remove current node
    if (left == null && right == null) {
    return null
    }
    if (left == null) {
    return right
    }
    if (right == null) {
    return left
    }
    }
    return root
    };

  • @pritam1366
    @pritam1366 3 ปีที่แล้ว +11

    this solution seems easy but doesn't come right away.

  • @varunshrivastava2706
    @varunshrivastava2706 3 ปีที่แล้ว +2

    Please man make your own dsa with python playlist. There aren't any resources available on TH-cam. You are a really good teacher. That playlist would be a big hit.

  • @joydeeprony89
    @joydeeprony89 ปีที่แล้ว

    My thought process was exactly same as yours, but while coding I was not able to write the algo , but I can say after following your videos I have learned a lot and improving everyday.

  • @hukunamutata
    @hukunamutata 2 ปีที่แล้ว +3

    Spent an hour trying to solve this just to end up coming here to watch you kill me with 10 lines of code

  • @bullsvip
    @bullsvip 3 ปีที่แล้ว +2

    Your drawings for thumbnail are top tier

  • @harpercfc_
    @harpercfc_ 2 ปีที่แล้ว

    A good day kicked off by the awesome solution and your explanation. Thank you so much!

  • @amegahed94
    @amegahed94 2 ปีที่แล้ว

    This solution is genius. I am wondering if you came up with it from the first time? My initial solution was a lot longer in terms of lines of code. Thanks NeetCode

  • @fawazolokodana6189
    @fawazolokodana6189 2 ปีที่แล้ว

    Your explanations are spectacular and easy to understand

  • @shantanushende6
    @shantanushende6 2 ปีที่แล้ว +11

    I still cant grasp where the actual trimming is happening!

    • @omarmk3420
      @omarmk3420 2 ปีที่แล้ว

      It’s not technically trimming, we are just not including the ones that fall out of the range hence the recursive calls

    • @arishsheikh3000
      @arishsheikh3000 ปีที่แล้ว

      You need to be good at recursion to understand these solutions

    • @frida8519
      @frida8519 ปีที่แล้ว

      try doing a run through the algo on a white board by hand! You should see how we're cutting off parts of the tree.

  • @hari8568
    @hari8568 9 หลายเดือนก่อน +1

    Does a BFS solution work here?This was my first thought but i can see the recursive nature

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

    What the hell, tree problems knock me out!

  • @manojmpatil1269
    @manojmpatil1269 3 ปีที่แล้ว +1

    Thank you

  • @iamnoob7593
    @iamnoob7593 4 หลายเดือนก่อน

    Thank u

  • @90skiddo93
    @90skiddo93 8 หลายเดือนก่อน

    I love neetcode

    • @90skiddo93
      @90skiddo93 8 หลายเดือนก่อน

      I tried this problem on my own but had trouble thinking about all the cases, i was losing train of thoughts. Is there any good method that can help me to work with this issue ?

  • @RobinHistoryMystery
    @RobinHistoryMystery 8 หลายเดือนก่อน

    oh wow, did not think of that

  • @SachinGupta-dn7wt
    @SachinGupta-dn7wt 2 ปีที่แล้ว +1

    best video

  • @stevenshrii
    @stevenshrii 2 ปีที่แล้ว

    I art search has no duplicates of elements

  • @anantmulchandani709
    @anantmulchandani709 2 ปีที่แล้ว

    Why have you returned the root node?