🌲 Tree Playlist: kzbin.info/www/bejne/hZ-2n2WOerZng7s
@alokesh9853 жыл бұрын
Out of all the leetcode solutions on KZbin, you write the cleanest, most understandable code. Please keep up the excellent work
@prithulbahukhandi17143 жыл бұрын
Not everyone has the ability to teach brother you have it. keep enlightening us. !!
@tonymontana92219 ай бұрын
I just want to add a comment for people who might not understand thoroughly. We are not simply skipping child nodes and reaching grandchild nodes. The max() here means we are carried over the largest value of the subtree with the grandchild node as the root for each grant child root. That is amazing. Thank a ton
@ArvindKonda2 жыл бұрын
Love it ! Elegant and the way you write your code with few lines at the bottom , few lines in the middle like finishing a puzzle. It's beautiful.
@raghavendharreddydarpally21253 жыл бұрын
your way of solving problem especially building from scratch and your explanation is very clear and good .please do more videos on most solved leetcode problems.
@lizgarcia4626 Жыл бұрын
It is incredible how comprehensible and organized your explanations and solutions are. Thank you so much!
@boopeshshanmugam34364 ай бұрын
There is so much work behind the video of choosing the right test cases to give the explanations in the best way. Thanks for your efforts. You rock !
@VY-zt3ph Жыл бұрын
This playlist is absolute gold. Don't know what I will expect in DP and Graphs.
@youlihanshu2 жыл бұрын
I really think whoever proposes this problem is definitely a genius! Such a problem with dfs, binary tree and dp, what a combination!
@eyou28133 жыл бұрын
Im dying at the corresponding number of Macauley Culkins in your House Robber series thumbnails, its just *chefs kiss*
@shuhaoliang1442 жыл бұрын
I feel that this problem requires the combination of Dynamic Programming and DFS (Recursion), and you explained this problem very clearly. Even though I am using Java and you are using Python, I followed your logic and did this problem correctly. Thank you very much for your fantastic teaching and explanation!
@aritralahiri83213 жыл бұрын
Very Nice and Simplified explanation . Really appreciate your work.
@ashishm88503 жыл бұрын
Beautifully explained. Thanks!
@lonen3rd Жыл бұрын
I did it using bfs, borrowing from the solution to House Robber II from collections import deque class Solution: def rob(self, root) -> int: def bfs(): rob1, rob2 = 0, 0 queue = deque() queue.append(root) while queue: n = 0 for i in range(len(queue)): # level order node = queue.popleft() n += node.val if node.left: queue.append(node.left) if node.right: queue.append(node.right) temp = max(rob1 + n, rob2) rob1 = rob2 rob2 = temp return rob2 return bfs()
@sushantrocks3 жыл бұрын
Bfs, store sum at each level and then just find max using alternate cells (0,2,4,...) (1,3,5,...). O(n) mem and O(n) time.
@prathamj22153 жыл бұрын
This is what I thought as well
@alokesh9853 жыл бұрын
This won't work for something like [2, 1, 3, null, 4]. Answer should be 7. This approach will return 6
@sushantrocks3 жыл бұрын
@@alokesh985 nice one
@aayush9080 Жыл бұрын
Great Explaination. Loved it. Have been watching your videos for the past 2 months. This explaination forced me to do away with my laziness and comment.
@dhaanaanjaay7 ай бұрын
I tried level order traversal and built an array list and then ran house robber 1 approach but it did not solve the edge cases. I hope one day I would be able to solve the way you do it!. Would you be able to explain what was your thought process to come to this solution
@yicheng77123 жыл бұрын
Thanks so much! Ur videos have been helped me a lot. #1 channel on my list for leetcode study. Looking forward to see more of your videos!
@shivambaghel96682 жыл бұрын
I start watching your channel when it was at 90k and now it's 95k ,(in approx. 1 month) ..Congratulation
@MsSkip603 жыл бұрын
Dude, this is some rockstar level content really. I can’t tell how grateful I’m and how motivating these are! Thanks a lot!
@arenmkhoyan11 ай бұрын
Very nice, thanks
@SairamDasari20009 ай бұрын
Are you fr? I thought I couldn't solve this problem until I saw your explanation. Thanks for making an impact 🙌
@thanirmalai5 ай бұрын
Amazing solution
@begula_chan7 ай бұрын
Thank you very much! This was really good
@anthonyuccello14582 жыл бұрын
This channel is the real leetcode premium
@RobinHistoryMystery7 ай бұрын
I tried using House Robber I + BFS for this problem one, two = 0, 0 queue = [root] while queue -> num = 0 for _ in queue -> node = queue.popleft() queue.push(node.left) queue.push(node.right) num += node.data one, two = two, max(one + num, two) return two
@kvtysАй бұрын
not enough circling at 8:00, failed to understand
@skylyyun Жыл бұрын
Thanks! very great explanation!
@amitarya4894Ай бұрын
Thank you
@HarshaVardhan-pj5gpАй бұрын
You are just amazing 😭❤️
@zacyou42202 жыл бұрын
what will be the time complexity and space complexity of this algo?
@___vandanagupta___ Жыл бұрын
Very well explained!!! .
@ladanafar59173 жыл бұрын
Please can you increase the frequency i.e can you do more questions?
@ananya___16252 жыл бұрын
Great explanation!!!
@BarneyBing8832 жыл бұрын
I was asked this question in an interview and I failed and I thought that it would use some complex data structure and algorithm, but your video proved me wrong! 😂 But honestly, thank you so much for explaining this!
@shaksham.222 жыл бұрын
What interview did you have?
@BarneyBing8832 жыл бұрын
@@shaksham.22 Rippling
@ambarishdashora54403 жыл бұрын
thank you so much bro. You gave an excellent explanation.
@anshhchaturvedi1155 Жыл бұрын
This approach is giving TLE in C++
@anshhchaturvedi1155 Жыл бұрын
Here's the code class Solution { public: pair help(TreeNode* root) { if(!root) return {0,0}; if(!root->left && !root->right) return{root->val,0}; return {root->val+help(root->left).second+help(root->right).second,(max(help(root->left).first,help(root->left).second)+max(help(root->right).first,help(root->right).second))}; } int rob(TreeNode* root) { return max(help(root).first,help(root).second); } }; It'd be great if someone could help
one question: when should we build a new nested funtion?
@jorgemejia15862 жыл бұрын
when you wanna pass a parameter that isn't given by the parent function
@danielsun7162 жыл бұрын
@@jorgemejia1586 But why can't we just set an variable, why should use a function? like we can just set n = 0 to change n why should we do it like def dfs(n) to change n?
@SmoothCode3 жыл бұрын
what does leftPair[1] and rightPair[1] even mean? The left most left node at the end of the binary tree?
@rohithv633 жыл бұрын
it represents the maximum score the robber can get if he decides not to rob the root node
@nikhilchauhan58373 жыл бұрын
Awesome content. can you cover some segment tree problems in future videos
@andreytamelo11832 жыл бұрын
Thanks!
@midhileshmomidi31202 жыл бұрын
For this input [2,1,3,null,4] ans is given as 7. Isn't it 6 tree : 2 1 3 4
@binit19922 жыл бұрын
Can we solve it using BFS ?
@ajinkya-wasnik Жыл бұрын
beautiful
@sangamsamdarshi4634 Жыл бұрын
recursion makes problems easy af
@midhileshmomidi31202 жыл бұрын
Can we do level order traversal on this
@sindhu5840 Жыл бұрын
for a skewed tree, level order traversal might not work. eg. 4 - 1 - 2 - 3 . level order will retun max 6 (4+2). but the robber can rob 4 and 3 and make a profit of 7
@joydeeprony89 Жыл бұрын
Smooth like Vaseline.
@shaharrefaelshoshany94423 жыл бұрын
the most perfect ever.
@adityarathi34202 жыл бұрын
Thanks you sir! :-)
@asdfasyakitori85142 жыл бұрын
Nice thumbnail
@kwaminaessuahmensah89203 жыл бұрын
god right here people
@whatuphere2 ай бұрын
Prabhu to the rescue!! _/\_
@PoojaMehta2713 жыл бұрын
Is it possible to solve this in an interview without coming across it even once? 🧐
@CertifiedDeadMemes3 жыл бұрын
yeah
@laplacesdemon81403 жыл бұрын
OMG ! genius
@jorgemejia15862 жыл бұрын
bro i thought you could use bfs on this one
@sourav_ghosh3 жыл бұрын
Picasso 🤏🤏
@skmdmobinulislam39042 жыл бұрын
when a teacher unlocks his god-level mode 🤯
@dhiwakarna8509 Жыл бұрын
Why are we helping the thief?
@coldstone872 жыл бұрын
why cant you chose 4 and 100? They aren't directly connected. This is an ambigious question with no solution and you just wrote a solution of your own version of question.
@shadowsw80204 ай бұрын
'The thief realized it forms a binary tree' wtf is this shit lmao
@nikhilgoyal007 Жыл бұрын
It did not pass for me. I copied it exactly but does not pass :( class Solution: def rob(self, root: Optional[TreeNode]) -> int: # return pair: [withroot, withoutroot] def dfs(root): if not root: return [0, 0] leftPair = dfs(root.left) rightPair = dfs(root.left) withRoot = root.val + leftPair[1] + rightPair[1] withoutRoot = max(leftPair) + max(rightPair) return [withRoot, withoutRoot] return max(dfs(root))
@amberfatima2777 Жыл бұрын
rightPair = dfs(root.left) this should be rightPair = dfs(root.right)
@@adityarathi3420 Hi , why is memoization needed ??? what is the repeated step here ?
@adityarathi34202 жыл бұрын
@@shivansh-gup without memoization i will getting tle on leetcode.
@shivansh-gup2 жыл бұрын
@@adityarathi3420 yeah I got the same but I used to think memoization is applied when we reapeatedly reach a given state in recursion. In this however we will traverse every node for once and there is no repeated state so why is there a need to memoize ? am I missing something ?
@officialnepalcricket Жыл бұрын
you are making it more complicated. You also draw too much and say tooooooo much. provide the idea first, then try to explain. This will make sense then. Otherwise, you are just trying to explain without giving how to do it and at last you tell this is the way you do it. That is not helping at all.
@akashdey56376 ай бұрын
why is BFS not working here? given below is my code: if not root: return root q = deque([root]) res = [] while q: val = [] for i in range(len(q)): node = q.popleft() val.append(node.val) if node.left: q.append(node.left) if node.right: q.append(node.right) res.append(val) rob1, rob2 = 0, 0 for i in range(len(res)): if not i % 2: rob1 += sum(res[i]) else: rob2 += sum(res[i]) return max(rob1, rob2)