当前位置:网站首页>House raiding 3
House raiding 3
2022-06-12 20:09:00 【Prodigal son's private dishes】
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
Code :
recursive
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int rob(TreeNode root) {
// if(root == null){
// return 0;
// }
// if(root.left == null && root.right == null){
// return root.val;
// }
// int val1 = root.val;
// // Consider parent node
// if(root.left != null){
// val1 += rob(root.left.left) + rob(root.left.right);
// }
// if(root.right != null){
// val1 += rob(root.right.left) + rob(root.right.right);
// }
// // Ignore parent node
// int val2 = rob(root.left) + rob(root.right);
// return Math.max(val1, val2);
Map<TreeNode, Integer> map = new HashMap<>();
return roll(root, map);
}
public int roll(TreeNode root, Map<TreeNode, Integer> map){
if(root == null){
return 0;
}
if(root.left == null && root.right == null){
return root.val;
}
if(map.containsKey(root)){
return map.get(root);
}
int val1 = root.val;
if(root.left != null){
val1 += roll(root.left.left, map) + roll(root.left.right, map);
}
if(root.right != null){
val1 += roll(root.right.left, map) + roll(root.right.right, map);
}
int val2 = roll(root.left, map) + roll(root.right, map);
int res = Math.max(val1, val2);
map.put(root, res);
return res;
}
}
Dynamic programming
Refer to someone else's code
class Solution {
public int rob(TreeNode root) {
int[] res = roll(root);
return Math.max(res[0], res[1]);
}
public int[] roll(TreeNode root){
int[] res = new int[2];
if(root == null){
return res;
}
int[] left = roll(root.left);
int[] right = roll(root.right);
res[0] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
res[1] = root.val + left[0] + right[0];
return res;
}
}
边栏推荐
- 进程的创建fork()、消亡wait()
- Detailed explanation of IO flow basic knowledge -- file and IO flow principle
- Demand and business model innovation - demand 1 - Introduction to demand engineering
- Bsn-ddc basic network introduction, technical features, unique advantages, application scenarios and platform access
- Demand and business model innovation - demand 2- demand basis
- Demand and business model innovation - demand 4- overview of demand acquisition
- exec函数、shell的实现
- What does SQL replace or
- 登錄mysql
- 用户权限和组权限
猜你喜欢

用户权限和组权限

测试必备:推荐一款跨平台App性能专项测试工具!

1. Getting to know R

Centos7 installing PHP

PostgreSQL数据库复制——后台一等公民进程WalReceiver pg_stat_wal_receiver视图

Storage system overview

Microsoft Word tutorial, how to insert page numbers and table of contents in word?

进程的创建fork()、消亡wait()

In 2022, FISCO bcos MVP recognized that the channel was open and invited you to become an open source leader

Demand and business model innovation-5-process
随机推荐
torch 网络模型转换onnx格式,并可视化
Why can't fields with high duplicate values be indexed (such as gender fields)
exec函数、shell的实现
基于微信电子书阅读小程序毕业设计毕设作品(2)小程序功能
Wechat applet notes
Explain
Golden, silver and four job hopping season, teach you these tips to improve the interview success rate
基于微信电子书阅读小程序毕业设计毕设作品(3)后台功能
Learning summary in March
Parameter meaning of random forest randomforestclassifier in sklearn
Ctfshow-web265 (deserialization)
Theory + practice will help you master the dynamic programming method
MySQL Basics
牛客網:三數之和
Bsn-ddc basic network introduction, technical features, unique advantages, application scenarios and platform access
MySQL log
What does SQL replace or
MySQL - the execution order of an SQL statement
Alipay payment episode 11: monitoring after successful payment callback
MySQL日志