当前位置:网站首页>Home raiding III (leetcode-337)
Home raiding III (leetcode-337)
2022-07-24 09:54:00 【Casten-Wang】
raid homes and plunder houses Ⅲ(LeetCode-337)
subject
The thief found a new area to steal . There is only one entrance to the area , We call it root .
except root outside , Each house has and only has one “ Father “ The house is 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 were robbed the same night , The house will alarm automatically .
Given a binary tree root . return Without triggering the alarm , The maximum amount a thief can steal .
Example 1:

Input : root = [3,2,3,null,3,null,1]
Output : 7
explain : The maximum amount a thief can steal in a night 3 + 3 + 1 = 7
Example 2:

Input : root = [3,4,5,1,3,null,1]
Output : 9
explain : The maximum amount a thief can steal in a night 4 + 5 = 9
Tips :
- The number of nodes in the tree is
[1, 104]Within the scope of 0 <= Node.val <= 104
Ideas
Tree array
Determine the parameters and return values of the recursive function
- Return the money obtained in two states of stealing and not stealing . Subscript 0 Indicates the maximum amount obtained by not stealing the current node , Subscript 1 Indicates the maximum amount obtained by stealing the current node
Determine termination conditions
- Empty node encountered , Must be returned { 0 , 0 } \{0,0\} { 0,0}
Determine the traversal order
- Must traverse in sequence , Because we need to consider after returning the value through the recursive function
Determine the single layer logic
If you steal the current node
- Left and right children can't steal , That is, the left and right children take the subscript 0 The value of the combined
v a l 1 = c u r . v a l + l e f t [ 0 ] + r i g h t [ 0 ] val1=cur.val+left[0]+right[0] val1=cur.val+left[0]+right[0]
- Left and right children can't steal , That is, the left and right children take the subscript 0 The value of the combined
If you don't steal the current node
- Left and right children can consider stealing , But whether to steal or not depends on
v a l 2 = m a x ( l e f t [ 0 ] , l e f t [ 1 ] ) + m a x ( r i g h t [ 0 ] , r i g h t [ 1 ] ) val2=max(left[0],left[1])+max(right[0],right[1]) val2=max(left[0],left[1])+max(right[0],right[1])
- Left and right children can consider stealing , But whether to steal or not depends on
The test case

Code display
class Solution
{
public:
int rob(TreeNode *root)
{
vector<int> result = robTree(root);
return max(result[0], result[1]);
}
vector<int> robTree(TreeNode *cur)
{
if (!cur)
{
return {
0, 0};
}
vector<int> curleft = robTree(cur->left);
vector<int> curright = robTree(cur->right);
int val1 = cur->val + curleft[0] + curright[0];
int val2 = max(curleft[0], curleft[1]) + max(curright[0], curright[1]);
return {
val2, val1};
}
};
边栏推荐
- RxJS Beginner Guide
- Color recognition of regions of interest in pictures and videos based on OpenCV
- [don't bother with reinforcement learning] video notes (I) 1. What is reinforcement learning?
- Get the historical quotation data of all stocks
- 聚集日志服务器
- Lung CT segmentation challenge 2017 dataset download and description
- When the hot tea is out of stock, what does the new tea drink rely on to continue its life?
- Getting started with identityserver4
- Mysql8.0 authorized remote login
- Write a simple memo using localstorage
猜你喜欢
![[STM32 learning] (22) STM32 realizes 360 degree rotary encoder](/img/8e/fb036296ec3aff5e60acee5018943c.png)
[STM32 learning] (22) STM32 realizes 360 degree rotary encoder
![[don't bother with reinforcement learning] video notes (I) 3. Why use reinforcement learning?](/img/57/0ebff0839d2a2898472d3270fd13df.png)
[don't bother with reinforcement learning] video notes (I) 3. Why use reinforcement learning?

Ask you to build a small program server

Spark Learning: using RDD API to implement inverted index

An article takes you to understand the operation of C language files in simple terms

The heads of the five major international institutions called for urgent action to deal with the global food security crisis

Spark Learning: build SQL to meet the specified optimization rules

What's the difference between testing / developing programmers' professionalism and salted fish? They don't want to be excellent coders?

Raspberry Pie: /bin/sh: 1: bison: not found
![Learn more about the synchronized lock upgrade process [concurrent programming]](/img/d8/a74d0e151aa16d4a02566a8a822285.png)
Learn more about the synchronized lock upgrade process [concurrent programming]
随机推荐
Common evaluation indexes of medical image segmentation
[STM32 learning] (14) two 74HC595 controls four nixie tube displays
[don't bother to strengthen learning] video notes (II) 1. What is Q-learning?
[STM32 learning] (16) STM32 realizes LCD1602 display (74HC595 drive) - 4-bit bus
ASI-20220222-Implicit PendingIntent
Write a simple memo using localstorage
Yarn: unable to load file
Cess test online line! The first decentralized storage network to provide multiple application scenarios
CAS principle [concurrent programming]
聚集日志服务器
How does SRE and development of Google cooperate
[MySQL] - deep understanding of index
PHP Basics - PHP types
Opencv learning Day5
OPENCV学习DAY5
[200 opencv routines] 236. Principal component analysis of feature extraction (openCV)
Can the "self-help master" who has survived the economic crisis twice continue to laugh this time?
PHP Basics - PHP magic method
Arduino drive Lora module node
What is the cloud native mid platform business architecture?