# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def rob(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        def search(node):
            if not node:    return 0, 0
            rob_left, not_rob_left = search(node.left)
            rob_right, not_rob_right = search(node.right)
            rob = not_rob_left + not_rob_right + node.val
            not_rob = max(rob_left, not_rob_left) + max(rob_right, not_rob_right)
            return rob, not_rob
        return max(search(root))