Тёмный

LeetCode Day 20 - Binary Search Tree from Preorder Traversal 

Errichto Algorithms
Подписаться 306 тыс.
Просмотров 28 тыс.
50% 1

Опубликовано:

 

2 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 112   
@mohitthorat8580
@mohitthorat8580 4 года назад
We want Tutorial on Trees Errichto!
@ajays861
@ajays861 4 года назад
A tutorial on Trees would be great!
@psn999100
@psn999100 4 года назад
The way this guy explains is plain superb !! Thanks
@CSBVASAMMANJUNATHKASHIRAM
@CSBVASAMMANJUNATHKASHIRAM 4 года назад
hell yes we need more of these tutorials btw great explanation
@k_co_KristidharPandit
@k_co_KristidharPandit 3 года назад
yaa we want BST tutorial , it will be a great entertainment to learn from you bruh , take love ✨
@ganumba11
@ganumba11 4 года назад
This was amazing explanation of binary search trees!! I love you
@bacuongcao2170
@bacuongcao2170 4 года назад
The problem here is you return a reference of a local variable to outer scope but the local variable will be deleted if it goes out of inner scope. P/s: great content :)
@sahilchoudhary1473
@sahilchoudhary1473 4 года назад
can you explain what are you saying
@bacuongcao2170
@bacuongcao2170 4 года назад
@@sahilchoudhary1473 In the video, Errichto got a runtime error while he is trying to return the address of the root node (a local variable) to the outer scope. The reason is that the root node which is a local variable went out of scope (the function is returned) and it's automatically released (destroyed) so you no longer access the returned value from the caller function.
@climbnexplore1187
@climbnexplore1187 4 года назад
@@bacuongcao2170 Thx for mentioning this. Do smart pointers help (?)
@bacuongcao2170
@bacuongcao2170 4 года назад
​@@climbnexplore1187 Of course, you can use a shared_ptr. But a normal pointer perfectly works in this case like what Errichto did in his video
@kunalsinghgusain2070
@kunalsinghgusain2070 4 года назад
@@bacuongcao2170 no I don't think this is the case because no matter if its local or global variable objects go to the heap, its stack that gets destroyed after method exit but variables stored in heap remain in the heap. so the method is just returning the reference to the object in the heap, which still exists. correct if I am wrong I am telling this from java point of view.
@hemantranjan2297
@hemantranjan2297 3 года назад
Please Take Leetcode 30 days challenge every month!! you are a gem!
@5590priyank
@5590priyank 4 года назад
A tutorial on various trees, like AVL tree, red-black tree, segment tree would be extremely helpful. There are very rare good tutorials on them
@ChandraShekhar-by3cd
@ChandraShekhar-by3cd 4 года назад
Hi Errichto . On Behalf of the coding community I would like to request if you could continue the LeetCode Challenge so that we can learn from it. It will be helpful for all of us as we had our previous Leetcode Monthly Challenge. Thanks
@jimwoodward7293
@jimwoodward7293 4 года назад
Another outstanding video Errichto!! Additional tutorials on BST, BFS and DFS would be great.
@BedoEbied
@BedoEbied 4 года назад
I really enjoyed it like a show! Go for these trees tutorial including recursion. Thanks
@baduguprakash6791
@baduguprakash6791 3 года назад
Yes we want data structures tutorial 😁
@Kranach777
@Kranach777 4 года назад
Thanks for showing O(n) solution. It was very interesting lesson.
@satviknema8629
@satviknema8629 4 года назад
14:49 "Global variable stuff like that BAD" lmao
@harishrawat1205
@harishrawat1205 4 года назад
Wow Errichto! You explained it beautifully. One thing i like the most is the way you explain how to approach a problem. Keep making such videos!
@uzdik.student
@uzdik.student 4 года назад
Thank you very much, didn't understand the code of O(n) solution, but drawing at the end of the video helped me to get the idea, now I can try to code it myself
@imdeadshot3632
@imdeadshot3632 4 года назад
would love to refer to tutorials on trees and graphs aftere 30-day challange, enjoyed the video!
@yunjiehong4649
@yunjiehong4649 4 года назад
As a Competitive programmer, you may be not familiar with ‘new’ operator.
@flamendless
@flamendless 4 года назад
Please keep the explanation (step by step + drawing) like in this video's end part. It's really helpful. Even if the problem is simple.
@AllmohtarifeBlogspot
@AllmohtarifeBlogspot 4 года назад
for my solution i used a stack to save the visited nodes, first i start by adding the root to the stack and for each element from the sequence if it's less than the top i do top->left = current. otherwise, i pop from the while the element values are small than the current value, eventually, there will be at least an element in the stack (at the end i add current to the right of the last element that had a value smaller than current)
@mihirvaghela3479
@mihirvaghela3479 4 года назад
Please make toturial on tree and recursion, *How you encounter recursion problem*
@SachinKumar-js8yd
@SachinKumar-js8yd 4 года назад
My first idea was similar to your second one... but faced issues in implementation. ***Tutorial on Trees*** would be great
@leetcodesolver9092
@leetcodesolver9092 4 года назад
I solved it in O(nlogn) using binary search inside recursion.
@ahsanulameensabit
@ahsanulameensabit 4 года назад
Enjoyed it 😊, please do videos on BST construction from other traversals...
@andreykarayvansky9549
@andreykarayvansky9549 4 года назад
The linear solution is very elegant. For some reason, my brain worked better with a stack than a recursion. I put on stack the first root (the result) and then if the next value is less than the stack top I put it on the stack, if not I pop from the stack to the moment it's either empty or the top is not less than the value and put again the new node on the stack.
@constructor365
@constructor365 4 года назад
Really appreciate you starting with the simpler solution first. Helps build intuition for the more complex solutions
@VARUN-pk7xq
@VARUN-pk7xq 4 года назад
How can I be like you ,what course should I do ,plz sir help me plz suggest me something ! That can I follow in daily basis to improve coding 🙏🙏🙏🙏🙏♥️♥️♥️♥️
@manmeetsingh1194
@manmeetsingh1194 4 года назад
Tutorial on Trees is needed too much
@ajithkk9762
@ajithkk9762 4 года назад
above in O(n) solution you put preorder[id] > limit for boundary check , would this be just id > limit ? @Errichto
@RAJPATEL-nm9nz
@RAJPATEL-nm9nz 4 года назад
I solved using 3 methods. 1)Using stack 2)recursion 3)sorting preorder to get inorder then constructing from them.
@kaustavpaul6625
@kaustavpaul6625 4 года назад
The third method defeats the purpose of the question. However, it's a nice trick. How did you solve using stack?
@shahirabdullah5438
@shahirabdullah5438 4 года назад
you are great
@samirallahverdi4948
@samirallahverdi4948 4 года назад
Very cooll explanation 👍😎
@abdurrubbyeinstein8480
@abdurrubbyeinstein8480 4 года назад
I want tutorial on segment trees
@huh_wtf
@huh_wtf 4 года назад
I solved it by regular insertion logic of BST. a simple recursion function
@taritgoswami3957
@taritgoswami3957 4 года назад
That's good, but, you are not using the fact that we have "the preorder traversal" in hand, not any random array.
@piotrgorski9786
@piotrgorski9786 4 года назад
But this solution is pessimistically quadratic.
@Shikharchaudhary007
@Shikharchaudhary007 4 года назад
Why not use binary search on the preorder vector and get a complexity of nlogn?
@hellowill
@hellowill 4 года назад
I did the same thing! So handy in C++ you can use int reference, had to wrap in an array in Java. Also isnt the space O(height)?
@barongrmel3797
@barongrmel3797 4 года назад
Yeah I think but then its also O(N) :)
@audiogear4412
@audiogear4412 4 года назад
Plus blue yeti sucks, get a pro audio gear, you deserve it. You are such a talent.
@__k.abhishek
@__k.abhishek 3 года назад
You are great. Very beautiful explanation :)
@deepakmehrotra3426
@deepakmehrotra3426 4 года назад
Can you make tutorial on the first problem of codejam round 1B(expogo)? Please...
@Naton
@Naton 4 года назад
logical but implementation is brain teasing
@shridharsarraf2188
@shridharsarraf2188 2 года назад
Vision/Jarvis is such a good programmer
@tonymok535
@tonymok535 4 года назад
I implemented it using iterative dfs, with upper and lower limit for each node. But it’s slower then your approach haha Ps just realised lower limit is not useful here
@rajarshibose5122
@rajarshibose5122 4 года назад
That's the key observation .I have done the same thing ,later realised that.
@subarukun8001
@subarukun8001 4 года назад
What Is the name of the program you use to draw?
@sirajummunir2001
@sirajummunir2001 4 года назад
Can I be Your Friend Please.....
@Thepankaz1
@Thepankaz1 3 года назад
How can you simply know that bounds will work like that, he didn't even try to break his approach. it simply worked like magic.
@sumanthgaduputi1485
@sumanthgaduputi1485 4 года назад
Tutorial on Graphs and trees please
@mr.mystiks9968
@mr.mystiks9968 4 года назад
The final solution you have is N time and N space but seems like Morris Tree Traversal algorithm makes space constant.
@chinmaykumar7975
@chinmaykumar7975 4 года назад
You are a very good teachrt Errichto
@abhirajsingh8138
@abhirajsingh8138 4 года назад
more trees and graph tuts plz'
@that_funny_guy496
@that_funny_guy496 4 года назад
As we were only asked to return BST root which will be formed from preorder traversal we can just write a insert() function and it works in O(n) in worst case.
@KP-nc9gk
@KP-nc9gk 4 года назад
"new" keyword makes the value stored in heap memory. So even if the variable holding it gets out of scope it won't get destroyed until manually deleted or the whole program is terminated.
@vishalmishra1937
@vishalmishra1937 4 года назад
hello sir could u sggest what to learn in maths for prograamming
@kruschiii
@kruschiii 4 года назад
Which keyboard layout are you using? American?
@Aswin255
@Aswin255 4 года назад
ayy i'm early.
@GDGET72
@GDGET72 4 года назад
Could we just use a Queue instead of using limit ? That's what it seems to be doing if we are just incrementing id++.
@sirajummunir2001
@sirajummunir2001 4 года назад
Why Don't You make tutorials on programming??
@mattdukeshire3837
@mattdukeshire3837 4 года назад
Code Jam vids?
@dumbassopinions2270
@dumbassopinions2270 2 года назад
best explanation
@akashtadwai9471
@akashtadwai9471 4 года назад
@Errictho maybe you can teach us some of your favorite topics from Computational Geometry after this series.
@sagarverma7640
@sagarverma7640 4 года назад
Awesome explanation.... hoping to see tutorials from on Tree Data Structures :)
@CarrotCakeMake
@CarrotCakeMake 4 года назад
It is fun watching you write these. I hope they give you some harder ones though, it is more fun when you don't know the answer right away.
@jadanabil8044
@jadanabil8044 3 года назад
We want Tree tutorial Errichto. Mercy, Mercy !!🙏
@shresthmishra9329
@shresthmishra9329 4 года назад
Blah Blah Blah
@alirezaamedeo
@alirezaamedeo 2 года назад
There's a mistake at 8:41 : The time complexity of inefficient algorithm in N*Log(N) not N**2.
@venkatsai5013
@venkatsai5013 4 года назад
Awesome explanation and thinking process _/\_
@SKstudio007
@SKstudio007 3 года назад
Write c program to implement binary search tree by inserting in following 6,2,8,10,4,3,7 write c routin for find operation min.max.delete please solve the. Problem
@ianpan0102
@ianpan0102 4 года назад
Fantastic video, I tried to wrap my head around the more advanced solution (2nd one) with various explanations, and your video finally did it!
@irsathkareem7513
@irsathkareem7513 4 года назад
yes, we want tutorials on trees and graphs..............
@brovet78
@brovet78 4 года назад
This is the best walkthrough I've seen on tree problems. Thanks!
@atibhiagrawal6460
@atibhiagrawal6460 4 года назад
I love your videos . Thanks so much for making them !
@fbru02
@fbru02 4 года назад
Yeah and a semgnet tree tutorial would be cool as well
@ritwikchakraborty1702
@ritwikchakraborty1702 4 года назад
More videos on tree and graphs please
@VinayKumar-rq5kd
@VinayKumar-rq5kd 4 года назад
same apporach as valid bst or not
@mrmrigank7154
@mrmrigank7154 3 года назад
erichto rocks
@jishnupramod2130
@jishnupramod2130 4 года назад
A series on Trees and Graphs would be great :)
@kabboghosh1853
@kabboghosh1853 4 года назад
tutorials on trees
@shashanktiwari4442
@shashanktiwari4442 4 года назад
Will u not upload codejam round 1B solutions?
@scayre4078
@scayre4078 4 года назад
really helpful tutorial , thx :D
@wengeance8962
@wengeance8962 4 года назад
I wish someone could help me with a python implementation of this algorithm, its very elegant
@bismeetsingh352
@bismeetsingh352 4 года назад
class Solution: def bstFromPreorder(self, preorder: List[int]) -> TreeNode: def helper(limit): nonlocal idx if idx==len(preorder) or preorder[idx]>limit: return None root_value=preorder[idx] root=TreeNode(root_value) idx+=1 root.left=helper(root_value) root.right=helper(limit) return root idx=0 return helper(float("inf"))
@ayush.kumar.13907
@ayush.kumar.13907 4 года назад
can such a method also be used for recreating BST from postorder, inorder traversal? what extra thing is needed for recreating tree from level-order traversal?
@Danzlh
@Danzlh 4 года назад
I am thinking the same thing. For some input, it should not be possible to produce a BST with the same preorder tranversal.
@srikanthnarapureddy4580
@srikanthnarapureddy4580 4 года назад
The problem can be simply solved in O(n) by performing BST insertion for every element from left to right of preorder array..
@PiyushKumar-qj8ue
@PiyushKumar-qj8ue 4 года назад
Think carefully, it won't be O(n). In worst case it will be O(n^2).
@srikanthnarapureddy4580
@srikanthnarapureddy4580 4 года назад
Yeah thanks for correcting me
@philtoa334
@philtoa334 4 года назад
YES
@rajdave9862
@rajdave9862 4 года назад
What is difference between low+high/2. And low +(high-low)/2 in binary search? Please tell
@jinxblaze
@jinxblaze 4 года назад
The latter does not cause integer overflow
@ajays861
@ajays861 4 года назад
ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-GU7DpgHINWQ.html Watch this video! the second formula will not cause an integer overflow.
@rajdave9862
@rajdave9862 4 года назад
@@ajays861 thank you so much, buddy ❣️, god bless you, thanks again.
@rajdave9862
@rajdave9862 4 года назад
@@jinxblaze thank you so much
@ajays861
@ajays861 4 года назад
@@rajdave9862 no mention 😇
@monitthakkar3173
@monitthakkar3173 4 года назад
make same video for only binary tree not bst
@audiogear4412
@audiogear4412 4 года назад
I can't stop laughing when I see TreeNode root = TreeNode(root_value)
@davithov
@davithov 6 месяцев назад
You're an amazing competitive programmer, but probably you should learn the c++ language a bit. For example, it was surprising to me that you didn't know about local variable's lilfetime and that it is an issue to return reference to it as it is already destroyed.
@satishkumarpatra4896
@satishkumarpatra4896 4 года назад
first cmnt.
@scepticgene
@scepticgene 4 года назад
Python please !!!
@shivaraj-bh
@shivaraj-bh 4 года назад
I have written a python3 implementation...O(N), it basically remembers the minimum possible value and maximum possible value at a given node and decides whether the current node fits in the position. check it out here: pastebin.com/MrNg1EiF