337 House Robber III
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 .
这题主要是得用一个result type把这层的结果和之前一层的结果记录下来。然后最后返回的时候,返回两者之间的最大值。中间更新的时候要注意,不跳过的时候,取左右两边的max加起来。跳过的时候就直接把root的value和孙子节点里的max加起来。T:O(n)要遍历所有节点,从下向上。S:O(height),递归栈的深度=树的高度。
Last updated