# Draw on DP thought: HOUSEROBBERIII

Source: Internet
Author: User

The thief has found himself a new place for his thievery again. There is a entrance to this area, called the "root." Besides the root, each house have one and only one parent house. After a tour, the Smart thief realized the ' all houses in this ' place forms a binary tree. It would automatically contact the police if and 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.

Main topic

Given a one or two-fork tree, the tree node has the right value, selecting some nodes that are not directly adjacent from the tree, making the node and the maximum

Thinking process

1. Simple rough, search is done, a node is only two cases, select or do not choose, in addition to the current node selection, the child node cannot be selected, if the current node is not selected, it can be divided into four cases

* Left selection, right not selected

* Right Select, left not selected

* Left and Right choice

* No choice left or right

2. write the code, OK but timeout (of course-_-later read the other answers, only to find too young)

3. because of the repeated calculation, and then to the dynamic planning considerations, and then came up with a state

D[0][1], where 0 indicates that the store iterates to the current node, taking the maximum value that the current node can reach, while 1 indicates that the maximum value that the current node can reach is not taken

Also because it is a tree node, so direct hash table storage d[treenode*][]

4. traversal sequence, habitual post-order traversal

5. Calculation rules

Obviously, the current node is taken, and the child node cannot take

D[treenode*][0] = Treenodeval + d[leftchild][1] + d[rightchild][1]

Four kinds of situations

D[TREENODE*][1] = max (d[leftchild][0] + d[rightchild][0], d[leftchild][1] + d[rightchild][0], d[leftchild][0] + d[ RIGHTCHILD][1], d[leftchild][1] + d[rightchild][1])

6. Finally, with the code; After reading the discussion, I thought it was too young, ╮(╯▽╰)╭.

`1 classSolution {2  Public:3     intRob (treenode*root) {4         if(Root = =NULL) {5             return 0;6         }7         8 Postorder (root);9         returnMax (d[root][0], d[root][1]);Ten     } One  A     voidPostorder (treenode*ITR) { -         if(ITR = =NULL) { -             return; the         } -  -Postorder (itr->Left ); -Postorder (itr->Right ); +  -Auto Ditr = D.insert (pair<treenode*, vector<int>> (ITR, vector<int> (2,0))); +Auto Leftitr = ditr.first->first->Left ; AAuto Rightitr = ditr.first->first->Right ; at  -         intRL = ditr.first->first->left! = NULL? d[ditr.first->first->left][0] :0; -         intRR = ditr.first->first->right! = NULL? d[ditr.first->first->right][0] :0; -         intUL = ditr.first->first->left! = NULL? d[ditr.first->first->left][1] :0; -         intUR = ditr.first->first->right! = NULL? d[ditr.first->first->right][1] :0; -  inditr.first->second[0] = Ditr.first->first->val + UL +UR; -ditr.first->second[1] = MAX (max (RL + UR, UL + RR), RL + RR), UL +UR); to     } +  - Private: theunordered_map<treenode*, vector<int>>D; *};`
View Code

Draw on DP thought: HOUSEROBBERIII

Related Keywords:

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.