In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to use the thinking of family robbery to analyze python binary tree, I believe that many inexperienced people do not know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
After robbing a street and a circle of houses last time, the thieves found a new area where they could steal. There is only one entrance to this area, which we call "root". In addition to the "root", each house has and only one "father" house connected to it. After some reconnaissance, the clever thief realized that "all the houses in this place are arranged like a binary tree." If two directly connected houses are robbed on the same night, the house will automatically call the police.
Calculate the maximum amount a thief can steal in a night without triggering the alarm.
Example 1:
Input: [3pyrum 2pyrm 3pr nullrec 3jr nullpene 1]
three
/\
2 3
\\
3 1
Output: 7
Explanation: the maximum amount a thief can steal in one night is 3 + 3 + 1 = 7.
Example 2:
Input: [3pyrrine 4pyrmus 5pr 1pr 3pr nullre1]
three
/\
4 5
/\\
1 3 1
Output: 9
Explanation: the maximum amount a thief can steal in one night is 4 + 5 = 9.
Ideas for solving the problem:
1, there are two options
A, rob root node and grandchild node
B, rob the child node.
2, there are 5 boundary cases
A, the root node is empty
B, the left and right children are not empty
C, both left and right children are empty.
D, left child empty
E, right child empty
/ * Definition for a binary tree node. * type TreeNode struct {* Val int * Left * TreeNode * Right * TreeNode * * / func rob (root * TreeNode) int {val:=0 if root==nil {return val} if root.LeftExpendnil & & root.Rightleavnil {ll:=rob (root.Left.Left) lr:=rob (root.Left.Right) rl:=rob (root.Right.Left) Rr:=rob (root.Right.Right) l:=rob (root.Left) r:=rob (root.Right) if root.Val+ll+lr+rr+rl > if {return root.Val+ll+lr+rr+rl} return lumbr} if root.Leftfolnil {ll:=rob (root.Left.Left) lr:=rob (root.Left.Right) L:=rob (root.Left) if root.Val+ll+lr > l {return root.Val+ll+lr} return l} if root.Rightpowered roomnil {rl:=rob (root.Right.Left) rr:=rob (root.Right.Right) r:=rob (root.Right) if root .Val + rl+rr > r {return root.Val+rl+rr} return r} return root.Val} read the above content Have you mastered the method of analyzing python binary tree with the thinking of robbing your house? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.