bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 337. House Robber III

337. House Robber III

Leetcode Solutions

Approach: Recursion

  1. Define a helper function that takes a node as an argument and returns an array of two integers.
  2. If the node is null, return [0, 0] as the base case.
  3. Recursively call the helper function for the left and right children of the current node.
  4. Calculate the money robbed when not robbing the current node by summing the maximum values of the left and right children's recursive calls.
  5. Calculate the money robbed when robbing the current node by adding the current node's value to the sum of the values when not robbing the left and right children.
  6. Return an array where the first element is the maximum money robbed when not robbing the current node, and the second element is the maximum money robbed when robbing the current node.
  7. In the main function, call the helper function with the root node and return the maximum of the two values in the returned array.
UML Thumbnail

Approach: Recursion with Memoization

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR