Range Sum of BST

  Рет қаралды 30,781

Kevin Naughton Jr.

Kevin Naughton Jr.

Күн бұрын

Пікірлер: 69
@preethiraajaratnam6399
@preethiraajaratnam6399 4 жыл бұрын
Hey Kevin, your videos are really helpful with interview prep! For this question, I felt an in-order traversal within the bounds L & R to be more intuitive than a BFS, just because it takes advantage of the fact that the tree is a BST.
@asimansubera960
@asimansubera960 5 жыл бұрын
Hello Kevin, Hope you are doing well! I am following your all LeetCode interview videos. Thank you for helping and educating me with your ideas and thought process solving each problem. These are very helpful.I would like to have a different video(s) from you with a holistic view of any interview question covering following aspects: 1. how you approach any problem 2. when to use Queue vs stack vs hashmap vs hashset vs arraylist, linkedlist, iterative vs recursion vs DP etc. 3. when to use one data structure vs multiple data structures 4. When to use an additional tracking data structure 5. when to use binary search when question is not about search though 6. when to use two indexes from both end of array I know it would be difficult to cover everything in a single video. You may create series of videos on this. Thanks in advance! -AB
@gideonkibetcheruiyot7500
@gideonkibetcheruiyot7500 5 жыл бұрын
your music is my favorite part of your videos, Kevin. Keep up the great work.
@KevinNaughtonJr
@KevinNaughtonJr 5 жыл бұрын
Gideon Kibet Cheruiyot thanks Gideon I really appreciate it!
@jasonfong2582
@jasonfong2582 5 жыл бұрын
Any idea what the music is? Seems familiar
@crimsoncad3230
@crimsoncad3230 4 жыл бұрын
Why can't we use Tree traversal algorithm like in-order traversal which will travel all the nodes ???
@ayushkashyap8299
@ayushkashyap8299 4 жыл бұрын
because there is a more efficient way of doing this. In which we don't have to traverse all nodes
@AnshumanKumar007
@AnshumanKumar007 4 жыл бұрын
@@ayushkashyap8299 we don't need to. We can check root.left if current.left > L and don't do it if it's not. We can do the same for root.right
@Tlacoyo59a
@Tlacoyo59a 4 жыл бұрын
I agree
@perpendicular355
@perpendicular355 3 жыл бұрын
@@Tlacoyo59a Space complexity would be O(N) in case we do recursive in order traversal.
@gerryramosftw
@gerryramosftw 5 жыл бұрын
amazing bro, really appreciate this video. will be watching more and probably all! glad I found your channel
@KevinNaughtonJr
@KevinNaughtonJr 5 жыл бұрын
Anytime and thanks Gerard I appreciate the support!
@stevenanthony4094
@stevenanthony4094 5 жыл бұрын
Hey Kevin, just wanted to say a quick thank you, your simple explanations have helped me tremendously.
@anushree3744
@anushree3744 5 жыл бұрын
Did not get why we didn't opt for DFS but BFS....
@leanobajio
@leanobajio 5 жыл бұрын
The choice is arbitrary. I would have used DFS since it doesn't need a queue to implement, so it uses less space.
@anushree3744
@anushree3744 5 жыл бұрын
@@leanobajio exactly
@KevinNaughtonJr
@KevinNaughtonJr 5 жыл бұрын
You can use either but bfs avoids using recursion which I think generally scales better (with dfs if you have a really large tree, you could run into a stack overflow)
@KevinNaughtonJr
@KevinNaughtonJr 5 жыл бұрын
Dean careful because dfs still uses extra space because of the recursive calls on the stack!
@leanobajio
@leanobajio 5 жыл бұрын
@@KevinNaughtonJr Oh yeah! I completely forgot about that.
@satrap299792458
@satrap299792458 4 жыл бұрын
Your music playlist is dope
@shivamsachan5109
@shivamsachan5109 5 жыл бұрын
can you explain the initialization of queue? (TreeNode & LinkedList)
@KevinNaughtonJr
@KevinNaughtonJr 5 жыл бұрын
SHIVAM SACHAN we wanted to make sure we store tree nodes in our queue so that inside our while loop we can have references to the current node’s children (we couldn’t do that if we stored something like integers in our queue) and linked list is just how you initialize queues in java (under the hood a queue is just a linked list with certain properties/abilities)
@lizdenhup
@lizdenhup 4 жыл бұрын
Thank you for making such an awesome video!! So clear! One question --why are you not making the condition on lines 24 and 27 greater than or equal to/less than or equal to, if the range is inclusive? Thanks. :)
@bhanuarora4504
@bhanuarora4504 2 жыл бұрын
can we solve this problem in O(logN)?
@aditya234567
@aditya234567 4 жыл бұрын
Nice but what's the point in tree being binary search tree. Interviewer would expect you to use that property!
@saurabhprasad2295
@saurabhprasad2295 4 жыл бұрын
Queue queue1 = new LinkedList(); The compiler saying.... its not declared in the scope.... please help....
@madiyar8079
@madiyar8079 5 жыл бұрын
More DP please!
@KevinNaughtonJr
@KevinNaughtonJr 5 жыл бұрын
Madiyar Kaskabayev you got it! I’ll try to do more dp problems thanks for the input!
@mdzikrullah9575
@mdzikrullah9575 4 жыл бұрын
Thank you for this video
@AmolGautam
@AmolGautam 3 жыл бұрын
Thank you so much for the videos.
@hichemrehouma5616
@hichemrehouma5616 5 жыл бұрын
Hey Kevin! Loving your videos! I just got off an onsite with Amazon and wanted to share a problem that I thought would be a good addition to your list. The problem says you have a list of arrays with 2 elements each, with first element being the name of the commander of the second element. Return a list with the names ordered by their rank from highest ranking to lowest ranking. I actually struggled a lot haha Lemme know your thoughts!
@ijaz2020
@ijaz2020 5 жыл бұрын
this is called topological sorting. google about it.
@naveenrawat6278
@naveenrawat6278 5 жыл бұрын
Topological sort does the trick.
@TonyDiCroce
@TonyDiCroce 2 жыл бұрын
I'm curious why you didn't do an in order traversal? Then you could at least stop once Val is greater than R?
@MrSriravi
@MrSriravi 5 жыл бұрын
Hey Kevin, thanks for your videos.Could you do a video on this problem "Permutation in String" LC-567.
@PatrickFong
@PatrickFong 4 жыл бұрын
why do you check the current val of the TreeNode, before you add the left and right nodes? Shouldn't you check the left / right node that they are within the range?
@shaanyahoo
@shaanyahoo 3 жыл бұрын
why not DFS ?
@alexeydeynega7603
@alexeydeynega7603 5 жыл бұрын
Can we use the information that this is binary search tree, and not just an arbitrary tree ? Maybe, this information will allow us to minimize number of nodes to be checked if we solve it recursively (not bfs)
@atift5465
@atift5465 5 жыл бұрын
Nice result and explanation! This is one of those fun problems that I found
@KevinNaughtonJr
@KevinNaughtonJr 5 жыл бұрын
atif tirmizi thanks Atif as always I appreciate the support!
@contact2ck
@contact2ck 5 жыл бұрын
Kevin kindly explain 489. Robot Room Cleaner. Leetcode problem
@iloveallparties
@iloveallparties 5 жыл бұрын
What does the average software engineering interview look like outside of companies like amazon, google, fb, etc? Do they all ask Leetcode?
@prageeth4855
@prageeth4855 5 жыл бұрын
For mobile dev positions specially 1. Ask to do a take home assignment or 2. Live coding of a component of an app.
@AndroidJames91
@AndroidJames91 5 жыл бұрын
To add to the mobile dev position interviews, I just finished about 10 of them. Steps: 1. Usually a recruiter call to determine if you have the buzz words they want for the position 2. I'm seeing a lot of discussion with hiring manager or a lead engineer for the team in this step. Usually asking you to describe projects, work experience, how you work with others, difficult bugs you've solved, and to get a feel for what you're looking for tech stack/team wise and if you can speak the technical jargon. 3. 1:1 technical screen with an engineer. These were usually LeetCode easy questions OR paired coding exercise on an Android app OR take home coding test (Usually about four hours of time investment required) and a one hour discussion with an engineer to discuss decisions made. 4. On-site, which is about 3-5 hours depending on company. Technical round with design questions, technical round with algorithms questions, and cultural fit. Might also have a group round with engineers on the team to do an informal discussion/walkthrough through designing a component/feature for the app. Most companies won't ask you LeetCode hards like top companies will, but they will ask Easy to Medium level LC questions. They'll test your design skills, any knowledge that is necessary to do the job (mobile, for instance), and team fit. Source: I worked at Amazon for over three years and participated in the process of interviewing candidates. I've also done a number of technical interviews in the last four months. The process described may vary for more junior positions as I was interviewing for mid to senior level roles.
@saurabhprasad2295
@saurabhprasad2295 4 жыл бұрын
superb man.... "You are the man"!!!!
@KevinNaughtonJr
@KevinNaughtonJr 4 жыл бұрын
thanks Saurabh!!!
@ashishkmr
@ashishkmr 5 жыл бұрын
Nice video, can you make more videos on leetcode hard/medium problems.
@KevinNaughtonJr
@KevinNaughtonJr 5 жыл бұрын
ashish kumar thanks Ashish and yes I’ll see what I can do!
@nishadkumar7322
@nishadkumar7322 5 жыл бұрын
+Kevin Naughton Jr. Could you please showcase problem # 1248 on LC (Count Number of Nice Subarrays) when you find time?? Thanks man.
@rahulsinghai3033
@rahulsinghai3033 5 жыл бұрын
Please solve some DP problem. I like your explanation
@KevinNaughtonJr
@KevinNaughtonJr 5 жыл бұрын
rahul singhai thanks Rahul and you got it!
@jsxica3751
@jsxica3751 4 жыл бұрын
The videos that go over this in Python aren't as high quality as yours, any chance you will do some of these in Python?
@pawlstothewall
@pawlstothewall 3 жыл бұрын
I wrote a recursive solution in python just now (where node is the root node). Any constructive criticism appreciated. def range_sum_bst(node, mini, maxi): if not node: return 0 if mini
@sanchitjain9745
@sanchitjain9745 3 жыл бұрын
Nice explanation... :)
@kmdr
@kmdr 4 жыл бұрын
Hey Kevin, why don't we check these two conditions when adding the left and right child to the queue: 1. current.val < L 2. current.val > R ?
@JohnLee-ob3fo
@JohnLee-ob3fo 3 жыл бұрын
Definition of BST (Binary Search Tree) The left subtree of a node contains only nodes with keys lesser than the node’s key. The right subtree of a node contains only nodes with keys greater than the node’s key. The left and right subtree each must also be a binary search tree.
@abhishekverma5928
@abhishekverma5928 4 жыл бұрын
Hey kevin, Thanks for your channel.It is really helpful. I have 1 doubt while adding left or right node in Queue you wrote if(current.left != null && current.val > L){ queue.add(current.left) } I think it should be current.left.val if(current.left != null && current.left.val >= L){ queue.add(current.left) } Also don't we need to check the upper bound condition also if(current.left != null && current.left.val >= L && current.left.val in case if Tree root node is start with 50, then we need to look both L and R in Tree's left and right node. Please correct me if I am wrong anywhere.
@sarveshchavan4391
@sarveshchavan4391 5 жыл бұрын
I live ur videos but just a suggestion if u can explain with diagram it will be really good!
@KevinNaughtonJr
@KevinNaughtonJr 5 жыл бұрын
sarvesh chavan thanks Sarvesh! Diagrams can be tricky but I’ll see what I can do!
@fridaytechtalk2452
@fridaytechtalk2452 4 жыл бұрын
Recursive Solution: public int rangeSumBST(TreeNode root, int L, int R) { if (root == null) return 0; if (root.val < L) return rangeSumBST(root.right, L, R); if (root.val > R) return rangeSumBST(root.left, L, R); return root.val + rangeSumBST(root.right, L, R) + rangeSumBST(root.left, L, R); }
@Ajayhanand
@Ajayhanand 5 жыл бұрын
I guess this needs segement trees
@Ajayhanand
@Ajayhanand 5 жыл бұрын
oh sorry i read it as an array range sum query
@prakad97
@prakad97 5 жыл бұрын
Hi Kevin, A video suggestion Leetcode #273 Number to Words..pls do if possible
@tlj77
@tlj77 5 жыл бұрын
public int rangeSumBST(TreeNode root, int L, int R){ int res = 0; if(root==null) return 0; if(root.valR) return rangeSumBST(root.left); res+=root.val; return rangeSumBST(root.right)+rangeSumBST(root.left)+res; }
@prageeth4855
@prageeth4855 5 жыл бұрын
Tks Kevin! 💯 Love to do a mock interview with u 😊
@KevinNaughtonJr
@KevinNaughtonJr 5 жыл бұрын
Prageeth Kumara anytime Prageeth! If you’re interested in a mock interview check out my Patreon page! The link is in the description!
@abhaytiwari6411
@abhaytiwari6411 5 жыл бұрын
hey kevin 83 number like is done my me
@marcgarcia3131
@marcgarcia3131 5 жыл бұрын
Hi Kevin, I have a tech interview for Google slated for the second week of December and would _love_ to pick your brain. I saw the Patreon link, so I'm just writing to get your attention and response, thanks! :)
Kth Smallest Element in a BST
9:19
Kevin Naughton Jr.
Рет қаралды 51 М.
Unique Paths
7:07
Kevin Naughton Jr.
Рет қаралды 60 М.
To Brawl AND BEYOND!
00:51
Brawl Stars
Рет қаралды 16 МЛН
Что-что Мурсдей говорит? 💭 #симбочка #симба #мурсдей
00:19
Симбочка Пимпочка
Рет қаралды 4,3 МЛН
How many people are in the changing room? #devil #lilith #funny #shorts
00:39
RANGE SUM OF A BST | PYTHON SOLUTION | LEETCODE 938
10:16
Cracking FAANG
Рет қаралды 2,9 М.
Minimum Path Sum
8:52
Kevin Naughton Jr.
Рет қаралды 57 М.
Binary Tree Right Side View
8:28
Kevin Naughton Jr.
Рет қаралды 41 М.
Word Search
8:46
Kevin Naughton Jr.
Рет қаралды 139 М.
Same Tree
6:45
Kevin Naughton Jr.
Рет қаралды 26 М.
DIAMETER OF A BINARY TREE | LEETCODE 543 | PYTHON DFS SOLUTION
10:02
An Entire Computer Science Degree in 11 Minutes
11:13
Kevin Naughton Jr.
Рет қаралды 858 М.
LeetCode Range Sum of BST Explained - Java
7:16
Nick White
Рет қаралды 16 М.
Reorganize String
12:44
Kevin Naughton Jr.
Рет қаралды 78 М.
To Brawl AND BEYOND!
00:51
Brawl Stars
Рет қаралды 16 МЛН