House Robber III

Medium

Company Tags

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called root.

In this new place, there are houses and each house has its only one parent house. All houses in this place form a binary tree. It will automatically contact the police if two directly-linked houses were broken.

You are given the root of the binary tree, return the maximum amount of money the thief can rob without alerting the police.

Example 1:

Input: root = [1,4,null,2,3,3]

Output: 7

Explanation: Maximum amount of money the thief can rob = 4 + 3 = 7

Example 2:

Input: root = [1,null,2,3,5,4,2]

Output: 12

Explanation: Maximum amount of money the thief can rob = 1 + 4 + 2 + 5 = 12

Constraints:

  • 1 <= The number of nodes in the tree <= 10,000.
  • 0 <= Node.val <= 10,000


Company Tags

Please upgrade to NeetCode Pro to view company tags.

root =