<LeetCode OJ> 337. House Robber III

Total Accepted: 1341 Total Submissions: 3744 Difficulty: Medium

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
/ \
4   5
/ \   \
1   3   1

Maximum amount of money the thief can rob = 4 + 5 = 9.

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int rob(TreeNode* root) {
if(root==NULL)
return 0;
queue<TreeNode*> que;//用来总是保存当层的节点
que.push(root);
int oddsum =root->val;//用于统计奇数层的和
int evensum=0;  //用于统偶数层的和
//获取每一层的节点
int curlevel=2;
while(!que.empty())
{
int levelSize = que.size();//通过size来判断当层的结束
for(int i=0; i<levelSize; i++)
{
if(que.front()->left != NULL) //先获取该节点下一层的左右子，再获取该节点的元素，因为一旦压入必弹出，所以先处理左右子
que.push(que.front()->left);
if(que.front()->right != NULL)
que.push(que.front()->right);
if(curlevel % 2 ==1)
oddsum  += que.front()->val;
else
evensum += que.front()->val;
que.pop();
}
curlevel++;
}
return oddsum > evensum ? oddsum : evensum;//奇数层的和与偶数层的和谁更大谁就是结果
}
};

int rob(TreeNode* root) {
int child = 0, childchild = 0;
rob(root, child, childchild);
return max(child, childchild);
}

void rob(TreeNode* root, int &child, int &childchild) {
if(!root) return;

int l1 = 0, l2 = 0, r1 = 0, r2 = 0;
rob(root->left, l1, l2);
rob(root->right, r1, r2);

child = l2 + r2 + root->val;
childchild = max(l1, l2) + max(r1, r2);
}

[leetcode-337]House Robber III(java)

• zdavb
• 2016年03月27日 22:19
• 825

[leetcode] 337. House Robber III

The thief has found himself a new place for his thievery again. There is only one entrance to this a...
• TstsUgeg
• 2016年03月12日 20:20
• 2327

LeetCode337. House Robber III

• codeTZ
• 2016年03月23日 20:34
• 1098

leetcode 337. House Robber III-动态规划|Java|Python简洁高效

• happyaaaaaaaaaaa
• 2016年03月13日 21:06
• 4641

337. House Robber III （C++实现）

• jingmiaa
• 2016年05月09日 18:41
• 423

leetCode198-House Robber

• Lu597203933
• 2015年04月01日 18:28
• 7397

Leetcode_198_House Robber

• pistolove
• 2015年08月15日 12:02
• 2108

[leetcode]337. House Robber III

• zorelemn
• 2016年04月13日 22:44
• 134

198. House Robber Leetcode Python

You are a professional robber planning to rob houses along a street. Each house has a certain amount...
• hyperbolechi
• 2015年03月31日 22:28
• 1258

leetcode 337. House Robber III

• jly0612
• 2016年07月05日 16:12
• 151

举报原因： 您举报文章： 337. House Robber III 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)