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

<p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p>

<p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" />
<pre>
<strong>Input:</strong> root = [3,2,3,null,3,null,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" />
<pre>
<strong>Input:</strong> root = [3,4,5,1,3,null,1]
<strong>Output:</strong> 9
<strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li>
	<li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li>
</ul>

'''
The thief has found himself a new place for his thievery again.

There is only one entrance to this area, called root.
- problem starts at root
- root is a recursive term
    - can solve sub-problems?

Besides the root, each house has one and only one parent house.

After a tour, the smart thief realized that all houses in this place form a binary tree.
- binary tree problem
- probably use DFS or BFS
    - repeated work though?

It will automatically contact the police if 
two directly-linked houses were broken into on the same night.
- cant rob root and root.left/root.right
    - remember to check if left and right exist before accessing
- picking problem:
    - rob root
    - dont rob root

Given the root of the binary tree, return 
the maximum amount of money the thief can rob without alerting the police.
- maximization problem

The number of nodes in the tree is in the range [1, 10^4].

0 <= Node.val <= 10^4

Input: root = [3,2,3,null,3,null,1]
Output: 7
- pick 3
    - skip its children (2, 3)
    - problem starts over again at 2 and 3s children
- skip 3
    - problem starts over again at 3s children


Input: root = root
Output: res = res
- pick root
    - skip left, skip right
    - root.val + f(left.left) + f(right.right)
- skip root
    - f(left) + f(right)

max(
    root.val + f(left.left) f(left.right) + f(right.left) + f(right.right),
    f(left) + f(right)
)
'''

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def rob(self, root: Optional[TreeNode]) -> int:
        cache = {}

        def maxProfitStartingAt(node: Optional[TreeNode]) -> int:
            if not node:
                return 0
            if node in cache:
                return cache[node]
            
            pickLeft = 0 if not node.left else maxProfitStartingAt(node.left.left) + maxProfitStartingAt(node.left.right)
            pickRight = 0 if not node.right else maxProfitStartingAt(node.right.left) + maxProfitStartingAt(node.right.right)

            pick = node.val + pickLeft + pickRight
            skip = maxProfitStartingAt(node.left) + maxProfitStartingAt(node.right)

            cache[node] = max(pick, skip)
            return max(pick, skip)
        
        return maxProfitStartingAt(root)
18700