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 };
@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.
@pritam13663 жыл бұрын
this solution seems easy but doesn't come right away.
@varunshrivastava27063 жыл бұрын
Please man make your own dsa with python playlist. There aren't any resources available on KZbin. You are a really good teacher. That playlist would be a big hit.
@harpercfc_2 жыл бұрын
A good day kicked off by the awesome solution and your explanation. Thank you so much!
@hari85689 ай бұрын
Does a BFS solution work here?This was my first thought but i can see the recursive nature
@amegahed94 Жыл бұрын
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
@fawazolokodana61892 жыл бұрын
Your explanations are spectacular and easy to understand
@bullsvip3 жыл бұрын
Your drawings for thumbnail are top tier
@hukunamutata2 жыл бұрын
Spent an hour trying to solve this just to end up coming here to watch you kill me with 10 lines of code
@shantanushende62 жыл бұрын
I still cant grasp where the actual trimming is happening!
@omarmk34202 жыл бұрын
It’s not technically trimming, we are just not including the ones that fall out of the range hence the recursive calls
@arishsheikh3000 Жыл бұрын
You need to be good at recursion to understand these solutions
@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.
@sidazhong2019 Жыл бұрын
What the hell, tree problems knock me out!
@manojmpatil12693 жыл бұрын
Thank you
@anantmulchandani7092 жыл бұрын
Why have you returned the root node?
@RobinHistoryMystery8 ай бұрын
oh wow, did not think of that
@stevenshrii2 жыл бұрын
I art search has no duplicates of elements
@90skiddo938 ай бұрын
I love neetcode
@90skiddo938 ай бұрын
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 ?