当前位置:网站首页>基础提升---树形DP补充
基础提升---树形DP补充
2022-06-10 17:44:00 【星光技术人】
基础提升---树形DP补充
题目1:最大距离


节点距离表示某两个节点之间的节点的个数;求以某节点为根节点的树中所有节点距离的最大距离
- 思路分析
节点A的最大距离有三种情况:
- 节点A参与
节点A左子树的最大深度+右子树的最大深度+1
2)节点A不参与
max(左子树最大距离,右子树最大距离)
所以树形DP写法需要返回两个信息:i.最大深度 ii.最大距离
- code
#include<iostream>
using namespace std;
struct TreeNode
{
TreeNode* left;
TreeNode* right;
};
class returntype
{
public:
int maxheight;
int max_dis;
returntype(int mh, int md)
{
maxheight = mh;
max_dis = md;
}
};
returntype get_dis(TreeNode* root)
{
if (root == nullptr)
{
return returntype(0, 0);
}
returntype L_info = get_dis(root->left);
returntype R_info = get_dis(root->right);
//列举可能性
//根节点参与
int p1 = L_info.maxheight;
int p2 = R_info.maxheight;
int p = p1 + p2 + 1;
int max_h = max(p1, p2) + 1;
//根节点不参与
int p3 = L_info.max_dis;
int p4 = R_info.max_dis;
int max_d = max(p, max(p3, p4));
return returntype(max_h, max_d);
}
题目2:宴会问题,打家劫舍



分析
同样仿照上题的思路,加入根节点为A,左右孩子分别为B和C;那么整个树的最大快乐值L_A由以下几个部分组成:
1) A参与情况下
节点A的值+节点B整棵树,不包含B的最大快乐值+节点B整棵树,不包含B的最大快乐值
2)A不参与的情况
0+max(B来,最大快乐值;B不来的最大快乐之值) + max(C来的最大快乐值;C不来的最大快乐值)code(写法一)
#include<iostream>
using namespace std;
struct TreeNode
{
int v;
TreeNode* left;
TreeNode* right;
};
class returntype
{
public:
int in_maxH;
int out_maxH;
returntype(int i, int o)
{
in_maxH = i;
out_maxH = o;
}
};
returntype get_maxhappy(TreeNode* root)
{
if (root == nullptr)
{
return returntype(0, 0);
}
returntype L_info = get_maxhappy(root->left);
returntype R_info = get_maxhappy(root->right);
//A参与
int p1 = root->v + L_info.out_maxH + R_info.out_maxH;
//A不参与
int p2 = max(L_info.in_maxH, L_info.out_maxH) + max(R_info.in_maxH, R_info.out_maxH);
int p = max(p1, p2);
return returntype(p1, p2);
}
- code2(写法二)
#include<iostream>
#include<vector>
using namespace std;
struct TreeNode
{
int v;
vector<TreeNode*> nexts;
};
class returntype
{
public:
int in_maxH;
int out_maxH;
returntype(int i, int o)
{
in_maxH = i;
out_maxH = o;
}
};
//写法二
returntype Get_maxhappy(TreeNode* root)
{
if (root->nexts.size()==0)
return returntype(root->v, 0);
int in_happy = root->v;
int out_happy = 0;
for (auto n : root->nexts)
{
in_happy += Get_maxhappy(n).in_maxH;
out_happy += max(Get_maxhappy(n).out_maxH, Get_maxhappy(n).in_maxH);
}
return returntype(in_happy, out_happy);
}
边栏推荐
- 掌握高性能计算前,我们先了解一下它的历史
- 使用DAP-Link单独下载可执行文件到MM32F5微控制器
- C语言在底层如何对double和float压栈
- [technical analysis] discuss the production process and technology of big world games - preliminary process
- 基于业务沉淀组件 =&gt; manage-table
- c语言---5 初识字符串、转义字符、注释
- Unity stepping on the pit record: if you inherit monobehavior, the constructor of the class may be called multiple times by unity. Do not initialize the constructor
- QtMqtt 源码编译设置KeepAlive后ping包超时错误不返回问题修复(QMQTT::MqttNoPingResponse,QMQTT::ClientPrivate::onPingTimeo)
- CDGA|工业企业进行数据治理的六个关键点
- Postman interface test tool
猜你喜欢

IIS installation and deployment web site

c语言---5 初识字符串、转义字符、注释

【技术分析】探讨大世界游戏的制作流程及技术——前期流程篇

Abbkine柱式法ExKine Pro动物细胞/组织总蛋白提取试剂盒

改变世界的开发者丨玩转“俄罗斯方块”的瑶光少年

Abbexa低样本量鸡溶菌酶 C (LYZ) ELISA 试剂盒

Abbexa CDAN1 siRNA使用说明书

Talk about those things about telecommuting, participate in the essay solicitation, receive the contribution fee and win the grand prize!

IP summary (tcp/ip volumes 1 and 2)

Canvas fire burning H5 animation JS special effects
随机推荐
Flutter在数字生活的发展与天翼云盘落地实践
c语言---11 分支语句if else
AOV网拓扑排序
CodeCraft-22 and Codeforces Round #795 (Div. 2)
攻防演练 | 网络安全“吹哨人”:安全监控
我在做一件很酷的事情
c语言---8 初识常见关键字
Library for adding progress bar during training --tqdm
一文带你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue
Set up an online help center to easily help customers solve problems
NaturalSpeech模型合成语音在CMOS测试中首次达到真人语音水平
c语言---9 初识宏、指针
换根呀呀啊呀
Leetcode 321. Nombre maximum de raccords
基于业务沉淀组件 =&gt; manage-table
The latest good article | interpretable confrontation defense based on causal inference
IIS installation and deployment web site
Wireshark learning notes (I) common function cases and skills
c语言---10 初识结构体
美学心得(第二百三十七集) 罗国正