Trim a Binary Search Tree - Leetcode 669 - Python
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
It’s always a good day when Neet uploads
this solution seems easy but doesn't come right away.
A good day kicked off by the awesome solution and your explanation. Thank you so much!
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.
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
};
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.
Your explanations are spectacular and easy to understand
Spent an hour trying to solve this just to end up coming here to watch you kill me with 10 lines of code
Thank you
Your drawings for thumbnail are top tier
Does a BFS solution work here?This was my first thought but i can see the recursive nature
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
Thank u
Why have you returned the root node?
I still cant grasp where the actual trimming is happening!
It’s not technically trimming, we are just not including the ones that fall out of the range hence the recursive calls
You need to be good at recursion to understand these solutions
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.
best video
oh wow, did not think of that
What the hell, tree problems knock me out!
I love neetcode
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 ?
I art search has no duplicates of elements