当前位置:网站首页>基础提升---树形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);
}
边栏推荐
- 完全背包问题以及优化小技巧
- Leetcode 321. Nombre maximum de raccords
- Analysis of transfer Refund Scheme in e-commerce industry
- 安装Linux系统的MySQL,在xshell中遇见的问题
- Noise line h5js effect realized by canvas
- This article introduces you to j.u.c's futuretask, fork/join framework and BlockingQueue
- 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
- XML & XPath parsing
- pwnable start
- Developers changing the world - Yao Guang teenagers playing Tetris
猜你喜欢

Faster Planner——Kinodynamic Astar详解

MYSQL开窗函数详解

(CVPR 2020) RandLA-Net: Efficient Semantic Segmentation of Large-Scale Point Clouds

凹印套印原理及影响套印的因素

LeetCode树经典题目(一)

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

afl-fuzz多线程

一文带你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue

Step on the pit. The BigDecimal was improperly used, resulting in P0 accident!

Abbexa 无细胞 DNA 试剂盒说明书
随机推荐
第七部分:第二课 顾问通用技能-如何安装和卸载SAP ERP系统客户端
堆利用之chunk extend: HITCON tranining lab13
4. ssh
c语言---5 初识字符串、转义字符、注释
Postman-接口测试工具
攻防演练 | 网络安全“吹哨人”:安全监控
Unity踩坑记录:如果继承MonoBehaviour,类的构造函数可能会被Unity调用多次,不要在构造函数做初始化工作
换根呀呀啊呀
XML & XPath parsing
Generate XML based on annotations and reflection
Set up an online help center to easily help customers solve problems
基于注解和反射生成xml
最新好文 | 基于因果推断的可解释对抗防御
Analysis of transfer Refund Scheme in e-commerce industry
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)
AOE网关键路径
canvas发散的粒子h5动画js特效
美学心得(第二百三十七集) 罗国正
仅需三步学会使用低代码ThingJS与森数据DIX数据对接