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 TH-cam. You are a really good teacher. That playlist would be a big hit.
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.
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
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 ?
It’s always a good day when Neet uploads
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
};
this solution seems easy but doesn't come right away.
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.
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.
Spent an hour trying to solve this just to end up coming here to watch you kill me with 10 lines of code
Your drawings for thumbnail are top tier
A good day kicked off by the awesome solution and your explanation. Thank you so much!
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
Your explanations are spectacular and easy to understand
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.
Does a BFS solution work here?This was my first thought but i can see the recursive nature
What the hell, tree problems knock me out!
Thank you
Thank u
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 ?
oh wow, did not think of that
best video
I art search has no duplicates of elements
Why have you returned the root node?