The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the “root.” 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 forms a binary tree”. It will automatically contact the police if two directly-linked houses were broken into on the same night.
Determine the maximum amount of money the thief can rob tonight without alerting the police.
Example 1:
Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
Example 2:
Maximum amount of money the thief can rob = 4 + 5 = 9.
解法1:
参考了这个帖子的解法3。 对于每一个节点,存在两种可能, 取或者不取。 那么可以用一个帮助函数,每一次计算的时候保存这两种情况的值。
用一个vector保存,v[0]表示从这个点出发,不包含这个点最大的可取值, v1表示从这个点出发并且包含这个点可取的最大值。
那么对于一个root, 分别计算left和right的vector,再按取或者不取的情况计算出root的vector。
如果取root的值,那一定不能取left和right的值, 所以最大取值是left1 + right1 + val
如果不取root的值,那可以取left和right的值, 这也等价于两颗树,对这两颗树取这个问题的最大值,就是left两种情况的最大值+right两种情况的最大值。
C++
|
|