(LeetCode OJ) 337. House Robber III

Source: Internet
Author: User

(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.

Credits:
Special thanks [email protected] adding this problem and creating all test cases.

Subscribeto see which companies asked this question

Show TagsShow Similar Problems

Analysis:

The answer below is wrong. I don't know where the error is !!! Isn't it possible to calculate the sum of the even and odd layers, and then compare the size of the result?

/*** 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
 
  
Que; // used to always save the node que at the layer. push (root); int oddsum = root-> val; // count the sum of int evensum = 0 on the odd layer; // used to unify the sum of the even layer // get the node int curlevel of each layer = 2; while (! Que. empty () {int levelSize = que. size (); // you can use the size parameter to determine the end of a layer. for (int I = 0; I
  
   
Left! = NULL) // obtain the left and right child of the next layer of the node, and then obtain the element of the node, because the left and right child que are processed first after the press is pushed. 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; // The sum of the odd and even layers and who are bigger are the results }};
  
 

Learn other people's code:

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);}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.