Trim a Binary Search Tree - Leetcode 669 - Python

Sdílet
Vložit
  • čas přidán 24. 07. 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    🐦 Twitter: / neetcode1
    🥷 Discord: / discord
    🐮 Support the channel: / neetcode
    ⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
    💡 CODING SOLUTIONS: • Coding Interview Solut...
    💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
    🌲 TREE PLAYLIST: • Invert Binary Tree - D...
    💡 GRAPH PLAYLIST: • Course Schedule - Grap...
    💡 BACKTRACKING PLAYLIST: • Word Search - Backtrac...
    💡 LINKED LIST PLAYLIST: • Reverse Linked List - ...
    💡 BINARY SEARCH PLAYLIST: • Binary Search
    📚 STACK PLAYLIST: • Stack Problems
    Problem Link: leetcode.com/problems/trim-a-...
    0:00 - Read the problem
    4:55 - Drawing Explanation
    9:18 - Coding Explanation
    leetcode 669
    This question was identified as an interview question from here: github.com/xizhengszhang/Leet...
    #coding #interview #python
    Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
  • Věda a technologie

Komentáře • 24

  • @freddy5638
    @freddy5638 Před 2 lety +21

    It’s always a good day when Neet uploads

  • @pritam1366
    @pritam1366 Před 2 lety +11

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

  • @harpercfc_
    @harpercfc_ Před 2 lety

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

  • @joydeeprony89
    @joydeeprony89 Před rokem

    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.

  • @halahmilksheikh
    @halahmilksheikh Před 2 lety +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
    };

  • @varunshrivastava2706
    @varunshrivastava2706 Před 2 lety +2

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

  • @fawazolokodana6189
    @fawazolokodana6189 Před rokem

    Your explanations are spectacular and easy to understand

  • @hukuna9957
    @hukuna9957 Před rokem +3

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

  • @manojmpatil1269
    @manojmpatil1269 Před 2 lety +1

    Thank you

  • @bullsvip
    @bullsvip Před 2 lety +2

    Your drawings for thumbnail are top tier

  • @hari8568
    @hari8568 Před 5 měsíci +1

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

  • @amegahed94
    @amegahed94 Před rokem

    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

  • @iamnoob7593
    @iamnoob7593 Před 6 dny

    Thank u

  • @anantmulchandani709
    @anantmulchandani709 Před 2 lety

    Why have you returned the root node?

  • @shantanushende6
    @shantanushende6 Před 2 lety +9

    I still cant grasp where the actual trimming is happening!

    • @omarmk3420
      @omarmk3420 Před 2 lety

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

    • @arishsheikh3000
      @arishsheikh3000 Před rokem

      You need to be good at recursion to understand these solutions

    • @frida8519
      @frida8519 Před rokem

      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.

  • @SachinGupta-dn7wt
    @SachinGupta-dn7wt Před 2 lety +1

    best video

  • @RobinHistoryMystery
    @RobinHistoryMystery Před 4 měsíci

    oh wow, did not think of that

  • @sidazhong2019
    @sidazhong2019 Před 9 měsíci +1

    What the hell, tree problems knock me out!

  • @90skiddo93
    @90skiddo93 Před 4 měsíci

    I love neetcode

    • @90skiddo93
      @90skiddo93 Před 4 měsíci

      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 ?

  • @stevenshrii
    @stevenshrii Před rokem

    I art search has no duplicates of elements