Recursion of course. But please make sure you cover all the cases.
class Solution(object): # return: max value with node picked, max val wo node picked def calcNode(self, node): if node is None: return 0, 0 il, el = self.calcNode(node.left) ir, er = self.calcNode(node.right) return node.val + el + er, max(il, el) + max(ir, er) def rob(self, root): ml, mr = self.calcNode(root) return max(ml, mr)