House Robber III - Tree - Leetcode 337

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ม.ค. 2025

ความคิดเห็น • 91

  • @NeetCode
    @NeetCode  4 ปีที่แล้ว +7

    🌲 Tree Playlist: th-cam.com/video/OnSn2XEQ4MY/w-d-xo.html

  • @alokesh985
    @alokesh985 3 ปีที่แล้ว +19

    Out of all the leetcode solutions on TH-cam, you write the cleanest, most understandable code. Please keep up the excellent work

  • @prithulbahukhandi1714
    @prithulbahukhandi1714 3 ปีที่แล้ว +38

    Not everyone has the ability to teach brother you have it. keep enlightening us. !!

  • @raghavendharreddydarpally2125
    @raghavendharreddydarpally2125 3 ปีที่แล้ว +7

    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.

  • @eyou2813
    @eyou2813 3 ปีที่แล้ว +3

    Im dying at the corresponding number of Macauley Culkins in your House Robber series thumbnails, its just *chefs kiss*

  • @lizgarcia4626
    @lizgarcia4626 ปีที่แล้ว +1

    It is incredible how comprehensible and organized your explanations and solutions are. Thank you so much!

  • @tonymontana9221
    @tonymontana9221 11 หลายเดือนก่อน +1

    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

    • @yiweiliu3529
      @yiweiliu3529 2 หลายเดือนก่อน

      If dfs results on nodes could be cached, the solution would be more straightforward. Instead of using cache, dfs results are broken down to avoid repeated dfs on children nodes. A smart workaround but it takes some time to figure out.

  • @VY-zt3ph
    @VY-zt3ph ปีที่แล้ว +1

    This playlist is absolute gold. Don't know what I will expect in DP and Graphs.

  • @andreytamelo1183
    @andreytamelo1183 2 ปีที่แล้ว

    Thanks!

  • @boopeshshanmugam3436
    @boopeshshanmugam3436 6 หลายเดือนก่อน

    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 !

  • @youlihanshu
    @youlihanshu 2 ปีที่แล้ว +1

    I really think whoever proposes this problem is definitely a genius! Such a problem with dfs, binary tree and dp, what a combination!

  • @ArvindKonda
    @ArvindKonda 2 ปีที่แล้ว +1

    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.

  • @yicheng7712
    @yicheng7712 3 ปีที่แล้ว +2

    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!

  • @aritralahiri8321
    @aritralahiri8321 3 ปีที่แล้ว +1

    Very Nice and Simplified explanation . Really appreciate your work.

  • @shuhaoliang144
    @shuhaoliang144 2 ปีที่แล้ว

    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!

  • @aayush9080
    @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.

  • @MsSkip60
    @MsSkip60 4 ปีที่แล้ว +3

    Dude, this is some rockstar level content really. I can’t tell how grateful I’m and how motivating these are! Thanks a lot!

  • @ashishm8850
    @ashishm8850 4 ปีที่แล้ว +3

    Beautifully explained. Thanks!

  • @sushantrocks
    @sushantrocks 3 ปีที่แล้ว +3

    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.

    • @prathamj2215
      @prathamj2215 3 ปีที่แล้ว

      This is what I thought as well

    • @alokesh985
      @alokesh985 3 ปีที่แล้ว +4

      This won't work for something like [2, 1, 3, null, 4]. Answer should be 7. This approach will return 6

    • @sushantrocks
      @sushantrocks 3 ปีที่แล้ว

      @@alokesh985 nice one

  • @lonen3rd
    @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()

  • @shivambaghel9668
    @shivambaghel9668 2 ปีที่แล้ว

    I start watching your channel when it was at 90k and now it's 95k ,(in approx. 1 month) ..Congratulation

  • @SairamDasari2000
    @SairamDasari2000 ปีที่แล้ว +1

    Are you fr? I thought I couldn't solve this problem until I saw your explanation. Thanks for making an impact 🙌

  • @dhaanaanjaay
    @dhaanaanjaay 9 หลายเดือนก่อน

    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

  • @anthonyuccello1458
    @anthonyuccello1458 2 ปีที่แล้ว

    This channel is the real leetcode premium

  • @kvtys
    @kvtys 3 หลายเดือนก่อน +1

    not enough circling at 8:00, failed to understand

  • @arenmkhoyan
    @arenmkhoyan ปีที่แล้ว +1

    Very nice, thanks

  • @HarshaVardhan-pj5gp
    @HarshaVardhan-pj5gp 3 หลายเดือนก่อน

    You are just amazing 😭❤️

  • @thanirmalai
    @thanirmalai 7 หลายเดือนก่อน

    Amazing solution

  • @begula_chan
    @begula_chan 9 หลายเดือนก่อน

    Thank you very much! This was really good

  • @skylyyun
    @skylyyun ปีที่แล้ว

    Thanks! very great explanation!

  • @yinglll7411
    @yinglll7411 3 ปีที่แล้ว

    Thank you for the great explanation!

  • @ambarishdashora5440
    @ambarishdashora5440 3 ปีที่แล้ว

    thank you so much bro. You gave an excellent explanation.

  • @BarneyBing883
    @BarneyBing883 2 ปีที่แล้ว

    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.22
      @shaksham.22 2 ปีที่แล้ว

      What interview did you have?

    • @BarneyBing883
      @BarneyBing883 2 ปีที่แล้ว

      @@shaksham.22 Rippling

  • @zacyou4220
    @zacyou4220 2 ปีที่แล้ว

    what will be the time complexity and space complexity of this algo?

  • @annoyingorange90
    @annoyingorange90 2 ปีที่แล้ว

    BEAUTIFUL SOLUTION

  • @ananya___1625
    @ananya___1625 2 ปีที่แล้ว

    Great explanation!!!

  • @RobinHistoryMystery
    @RobinHistoryMystery 9 หลายเดือนก่อน

    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

  • @___vandanagupta___
    @___vandanagupta___ 2 ปีที่แล้ว

    Very well explained!!! .

  • @s.z.7938
    @s.z.7938 2 ปีที่แล้ว

    Amazing, really helpful, thank you!!!

  • @amitarya4894
    @amitarya4894 3 หลายเดือนก่อน

    Thank you

  • @ladanafar5917
    @ladanafar5917 3 ปีที่แล้ว

    Please can you increase the frequency i.e can you do more questions?

  • @midhileshmomidi3120
    @midhileshmomidi3120 2 ปีที่แล้ว

    For this input [2,1,3,null,4] ans is given as 7. Isn't it 6
    tree : 2
    1 3
    4

  • @ritikkhatri
    @ritikkhatri 3 ปีที่แล้ว

    such clear explanation

  • @anshhchaturvedi1155
    @anshhchaturvedi1155 ปีที่แล้ว

    This approach is giving TLE in C++

    • @anshhchaturvedi1155
      @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

    • @tarunsahu1366
      @tarunsahu1366 5 หลายเดือนก่อน

      @@anshhchaturvedi1155
      class Solution {
      public:
      vector func(TreeNode* root){
      if(root == NULL) return {0, 0};
      vector leftPair = func(root->left);
      vector rightPair = func(root->right);
      int withRoot = root->val + leftPair[1] + rightPair[1];
      int withoutRoot = max(leftPair[0], leftPair[1]) + max(rightPair[0], rightPair[1]);
      return {withRoot, withoutRoot};
      }
      int rob(TreeNode* root) {
      vector result = func(root);
      return max(result[0], result[1]);
      }
      };

  • @aditijain2448
    @aditijain2448 ปีที่แล้ว

    Will this solution be enough in an interview

  • @edwardteach2
    @edwardteach2 3 ปีที่แล้ว +1

    U a God

  • @hoanganhtu9090
    @hoanganhtu9090 3 ปีที่แล้ว

    great explaination

  • @nikhilchauhan5837
    @nikhilchauhan5837 4 ปีที่แล้ว

    Awesome content. can you cover some segment tree problems in future videos

  • @danielsun716
    @danielsun716 2 ปีที่แล้ว

    one question: when should we build a new nested funtion?

    • @jorgemejia1586
      @jorgemejia1586 2 ปีที่แล้ว

      when you wanna pass a parameter that isn't given by the parent function

    • @danielsun716
      @danielsun716 2 ปีที่แล้ว

      @@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?

  • @SmoothCode
    @SmoothCode 3 ปีที่แล้ว

    what does leftPair[1] and rightPair[1] even mean? The left most left node at the end of the binary tree?

    • @rohithv63
      @rohithv63 3 ปีที่แล้ว

      it represents the maximum score the robber can get if he decides not to rob the root node

  • @midhileshmomidi3120
    @midhileshmomidi3120 2 ปีที่แล้ว

    Can we do level order traversal on this

    • @sindhu5840
      @sindhu5840 2 ปีที่แล้ว

      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

  • @binit1992
    @binit1992 2 ปีที่แล้ว

    Can we solve it using BFS ?

  • @ajinkya-wasnik
    @ajinkya-wasnik ปีที่แล้ว

    beautiful

  • @asdfasyakitori8514
    @asdfasyakitori8514 3 ปีที่แล้ว +1

    Nice thumbnail

  • @shaharrefaelshoshany9442
    @shaharrefaelshoshany9442 3 ปีที่แล้ว

    the most perfect ever.

  • @adityarathi3420
    @adityarathi3420 2 ปีที่แล้ว

    Thanks you sir! :-)

  • @joydeeprony89
    @joydeeprony89 2 ปีที่แล้ว

    Smooth like Vaseline.

  • @sangamsamdarshi4634
    @sangamsamdarshi4634 ปีที่แล้ว

    recursion makes problems easy af

  • @whatuphere
    @whatuphere 4 หลายเดือนก่อน

    Prabhu to the rescue!! _/\_

  • @PoojaMehta271
    @PoojaMehta271 3 ปีที่แล้ว +1

    Is it possible to solve this in an interview without coming across it even once? 🧐

  • @coldstone87
    @coldstone87 2 ปีที่แล้ว

    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.

  • @sourav_ghosh
    @sourav_ghosh 3 ปีที่แล้ว +1

    Picasso 🤏🤏

  • @TrippyJRE
    @TrippyJRE 3 ปีที่แล้ว

    OMG ! genius

  • @kwaminaessuahmensah8920
    @kwaminaessuahmensah8920 3 ปีที่แล้ว

    god right here people

  • @jorgemejia1586
    @jorgemejia1586 2 ปีที่แล้ว

    bro i thought you could use bfs on this one

  • @nikhilgoyal007
    @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
      @amberfatima2777 ปีที่แล้ว

      rightPair = dfs(root.left) this should be
      rightPair = dfs(root.right)

  • @skmdmobinulislam3904
    @skmdmobinulislam3904 2 ปีที่แล้ว

    when a teacher unlocks his god-level mode 🤯

  • @WowPlusWow
    @WowPlusWow 3 ปีที่แล้ว

    🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @dhiwakarna8509
    @dhiwakarna8509 2 ปีที่แล้ว

    Why are we helping the thief?

  • @shadowsw8020
    @shadowsw8020 6 หลายเดือนก่อน

    'The thief realized it forms a binary tree' wtf is this shit lmao

  • @alishan9354
    @alishan9354 3 ปีที่แล้ว

    please start coding in c++

    • @adityarathi3420
      @adityarathi3420 2 ปีที่แล้ว +2

      class Solution {
      public:

      map map ;

      int rob(TreeNode* root) {

      if(not root) return 0 ;

      if(map.count(root)) return map[root] ;

      int withoutRoot = rob(root->left) + rob(root->right) ;
      int withRoot = root->val ;

      if(root->left) withRoot += rob(root->left->left) + rob(root->left->right) ;
      if(root->right) withRoot += rob(root->right->left) + rob(root->right->right) ;


      return map[root] = max(withRoot,withoutRoot) ;

      }
      };

    • @shivansh-gup
      @shivansh-gup 2 ปีที่แล้ว

      @@adityarathi3420 Hi , why is memoization needed ??? what is the repeated step here ?

    • @adityarathi3420
      @adityarathi3420 2 ปีที่แล้ว

      @@shivansh-gup without memoization i will getting tle on leetcode.

    • @shivansh-gup
      @shivansh-gup 2 ปีที่แล้ว

      @@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 ?

  • @akashdey5637
    @akashdey5637 8 หลายเดือนก่อน

    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)

  • @salahabdelrahman7820
    @salahabdelrahman7820 4 หลายเดือนก่อน

    Great Explaination