Trim a Binary Search Tree - Leetcode 669 - Python

  Рет қаралды 18,871

NeetCode

NeetCode

Күн бұрын

Пікірлер: 24
@freddy5638
@freddy5638 3 жыл бұрын
It’s always a good day when Neet uploads
@halahmilksheikh
@halahmilksheikh 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 };
@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.
@pritam1366
@pritam1366 3 жыл бұрын
this solution seems easy but doesn't come right away.
@varunshrivastava2706
@varunshrivastava2706 3 жыл бұрын
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_
@harpercfc_ 2 жыл бұрын
A good day kicked off by the awesome solution and your explanation. Thank you so much!
@hari8568
@hari8568 9 ай бұрын
Does a BFS solution work here?This was my first thought but i can see the recursive nature
@amegahed94
@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
@fawazolokodana6189
@fawazolokodana6189 2 жыл бұрын
Your explanations are spectacular and easy to understand
@bullsvip
@bullsvip 3 жыл бұрын
Your drawings for thumbnail are top tier
@hukunamutata
@hukunamutata 2 жыл бұрын
Spent an hour trying to solve this just to end up coming here to watch you kill me with 10 lines of code
@shantanushende6
@shantanushende6 2 жыл бұрын
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.
@sidazhong2019
@sidazhong2019 Жыл бұрын
What the hell, tree problems knock me out!
@manojmpatil1269
@manojmpatil1269 3 жыл бұрын
Thank you
@anantmulchandani709
@anantmulchandani709 2 жыл бұрын
Why have you returned the root node?
@RobinHistoryMystery
@RobinHistoryMystery 8 ай бұрын
oh wow, did not think of that
@stevenshrii
@stevenshrii 2 жыл бұрын
I art search has no duplicates of elements
@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 ?
@iamnoob7593
@iamnoob7593 4 ай бұрын
Thank u
@SachinGupta-dn7wt
@SachinGupta-dn7wt 2 жыл бұрын
best video
Matchsticks to Square - Leetcode 473 - Python
15:48
NeetCode
Рет қаралды 28 М.
Binary Search Tree Iterator - Leetcode 173 - Python
12:47
NeetCode
Рет қаралды 42 М.
Мама у нас строгая
00:20
VAVAN
Рет қаралды 10 МЛН
Twin Telepathy Challenge!
00:23
Stokes Twins
Рет қаралды 94 МЛН
Delete and Earn - Dynamic Programming - Leetcode 740 - Python
19:09
Validate Binary Search Tree - Leetcode 98 - Trees (Python)
8:12
Minimum Height Trees - Leetcode 310 - Python
23:30
NeetCodeIO
Рет қаралды 21 М.
Can Place Flowers - Leetcode 605 - Python
10:34
NeetCode
Рет қаралды 48 М.
Climbing Stairs - Dynamic Programming - Leetcode 70 - Python
18:08
Binary Search Tree in Python
22:59
NeuralNine
Рет қаралды 54 М.
LOWEST COMMON ANCESTOR OF A BINARY TREE I | PYTHON | LEETCODE 236
12:48
3 Types of Algorithms Every Programmer Needs to Know
13:12
ForrestKnight
Рет қаралды 500 М.
Construct String from Binary Tree - Leetcode 606 - Python
10:00