ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码

337 House Robber III

来源:网络整理     时间:2016-04-11     关键词:house

本篇文章主要介绍了"337 House Robber III",主要涉及到house方面的内容,对于Javajrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播感兴趣的同学可以参考一下: The thief has found himself a new place for his thievery again. There is only on...

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:

3
    / \
   2   3
    \   \ 
     3   1
Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.

Example 2:

     3
    / \
   45
  / \   \ 
 1   3   1
Maximum amount of money the thief can rob = 4 + 5 = 9.

思路:此时形状又是二叉树型的了,就要换一种思路了,dp算法已经在这不适用了。

此时要设置一个标志位,标示它的父节点有没有被选中

1 如果父节点被选中了,那么当前节点就不能选 max=rob(root.left,false)+rob(root.right,false);

2 如果父节点没有被选中,那么当前节点就是Math.max(rob(root.left,true)+rob(root.right,true)+root.val,rob(root.left,false)+rob(root,right,false));

代码如下(已通过leetcode)

以上就介绍了337 House Robber III,包括了house方面的内容,希望对Javajrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播有兴趣的朋友有所帮助。

本文网址链接:http://www.codes51.com/article/detail_647362.html

相关图片

相关文章