体系班第十三节

1判断完全二叉树递归做法

有四种情况:1 左树完全,右数满,且左高为右高加一

2左满 ,右满,左高为右高加一

3左满,右完全,左右高相等

4左右均满且高相等

#include<iostream>
#include<algorithm>
using namespace std;
class TreeNode {
public:int val;TreeNode* left;TreeNode* right;TreeNode(int a) :val(a), left(nullptr), right(nullptr) {};
};
struct info {int height;bool iscbt;bool isfull;info(int a,bool b,bool c):height(a),iscbt(b),isfull(c){}
};
info process(TreeNode* head)
{if (head == nullptr)return info(0, true, true);info leftinfo = process(head->left);info rightinfo = process(head->right);int height = max(leftinfo.height, rightinfo.height) + 1;bool isfull = leftinfo.isfull && rightinfo.isfull && leftinfo.height == rightinfo.height;bool iscbt = false;if (leftinfo.isfull && rightinfo.isfull && leftinfo.height - rightinfo.height == 1)iscbt = true;if (leftinfo.isfull && rightinfo.isfull && leftinfo.height ==rightinfo.height )iscbt = true;if (leftinfo.iscbt && rightinfo.isfull && leftinfo.height - rightinfo.height == 1)iscbt = true;if (leftinfo.isfull && rightinfo.iscbt && leftinfo.height == rightinfo.height)iscbt = true;return info(height, iscbt, isfull);
}
bool iscbt(TreeNode* head)
{if (head == nullptr)return true;return process(head).iscbt;
}

2 给定一棵二叉树的头节点head,返回这颗二叉树中最大的二叉搜索子树的头节点

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class TreeNode {
public:int val;TreeNode* left;TreeNode* right;TreeNode(int data) : val(data), left(nullptr), right(nullptr) {}
};
struct info {TreeNode* node;//最大搜索子树头结点int maxsize;int min;int max;info(TreeNode *a,int b,int c,int d):node(a),maxsize(b),min(c),max(d){}
};
info* process(TreeNode* head)
{if (head == nullptr)return nullptr;info* leftinfo = process(head->left);info* rightinfo = process(head->right);int maxval = head->val;int minval = head->val;TreeNode* ans = nullptr;int size = 0;if (leftinfo != nullptr){maxval = max(maxval, leftinfo->max);minval = min(minval, leftinfo->min);ans = leftinfo->node;size = leftinfo->maxsize;}if (rightinfo != nullptr){maxval = max(maxval, rightinfo->max);minval = min(minval, rightinfo->min);if (rightinfo->maxsize > size){ans = rightinfo->node;size = rightinfo->maxsize;}}//当能构成搜索二叉树时if((leftinfo==nullptr?true:(leftinfo->node==head->left&&leftinfo->max<head->val))&& (rightinfo == nullptr ? true : (rightinfo->node == head->right && rightinfo->min > head->val))){ans=head;//一定要记得判空size = (leftinfo == nullptr ? 0 : leftinfo->maxsize) + (rightinfo == nullptr ? 0 : leftinfo->maxsize) + 1;}return new info(ans, size, minval, maxval);
}
TreeNode* maxSubBSTHead2(TreeNode* head)
{if (head == nullptr)return nullptr;return process(head)->node;
}

3给定一棵二叉树的头节点head,和另外两个节点a和b,返回a和b的最低公共祖先

法1:用哈希表记下所有节点父节点,将一个节点不停地向上,这其中经过的节点放入一个集合中

再在另一个节点从上遍历,一遍查找是否在集合中已经存在过,找到的第一个即为答案

#include <iostream>
#include <unordered_map>
#include <unordered_set>
using namespace std;
class TreeNode {
public:int val;TreeNode* left;TreeNode* right;TreeNode(int data) : val(data), left(nullptr), right(nullptr) {}
};
void fillmap(TreeNode* head, unordered_map<TreeNode*, TreeNode*> &map)
{if (head->left != nullptr){map[head->left] = head;fillmap(head->left, map);}if (head->right != nullptr){map[head->right] = head;fillmap(head->right, map);}
}
TreeNode* lowestAncestor(TreeNode* head, TreeNode* p, TreeNode* q)
{if (head == nullptr)return nullptr;unordered_map<TreeNode*, TreeNode*> map;//记录所有节点的父节点map[head] = nullptr;fillmap(head,map);unordered_set<TreeNode*> set;TreeNode* cur = p;set.insert(cur);while (map[cur] != nullptr){cur = map[cur];set.insert(cur);}cur = q;while (set.find(cur) == set.end()){cur = map[cur];}return cur;
}

法二:递归套路,会聚点与x有关还是无关:无关:已经在左或右树右答案,或这棵树a,b没找全

x是答案:左发现一个,右发现一个

x本身就是a,然后左右发现了b   x本身就是b,左右发现a

#include <iostream>
using namespace std;// 节点类
class Node {
public:int value;Node* left;Node* right;Node(int data) : value(data), left(nullptr), right(nullptr) {}
};
// 最低公共祖先函数
Node* lowestAncestor2(Node* head, Node* a, Node* b) {return process(head, a, b).ans;
}// 信息结构体
struct Info {bool findA;bool findB;Node* ans;Info(bool fA, bool fB, Node* an) : findA(fA), findB(fB), ans(an) {}
};// 处理函数
Info process(Node* x, Node* a, Node* b) {if (x == nullptr) {return Info(false, false, nullptr);}Info leftInfo = process(x->left, a, b);Info rightInfo = process(x->right, a, b);bool findA = (x == a) || leftInfo.findA || rightInfo.findA;//不要忘了x本身就是a的情况bool findB = (x == b) || leftInfo.findB || rightInfo.findB;Node* ans = nullptr;if (leftInfo.ans != nullptr) {ans = leftInfo.ans;}else if (rightInfo.ans != nullptr) {ans = rightInfo.ans;}else {if (findA && findB) {ans = x;}}return Info(findA, findB, ans);
}

4  派对的最大快乐值
 员工信息的定义如下:
class Employee {
    public int happy; // 这名员工可以带来的快乐值
    List<Employee> subordinates; // 这名员工有哪些直接下级
}
公司的每个员工都符合 Employee 类的描述。整个公司的人员结构可以看作是一棵标准的、 没有环的多叉树
树的头节点是公司唯一的老板,除老板之外的每个员工都有唯一的直接上级
叶节点是没有任何下属的基层员工(subordinates列表为空),除基层员工外每个员工都有一个或多个直接下级
这个公司现在要办party,你可以决定哪些员工来,哪些员工不来,规则:
1.如果某个员工来了,那么这个员工的所有直接下级都不能来
2.派对的整体快乐值是所有到场员工快乐值的累加
3.你的目标是让派对的整体快乐值尽量大
给定一棵多叉树的头节点boss,请返回派对的最大快乐值。

分情况:x来,x不来,定义一个结构体,保存两个值,x来时候的最大值,x不来时候的最大值

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct employee {int happy;vector<employee*> nexts;employee(int h):happy(h),nexts(){}
};
struct info {int yes;int no;info(int a,int b):yes(a),no(b){}
};
info process(employee* head)
{if (head == nullptr)return info(0, 0);int yes = head->happy;int no = 0;for (employee* a : head->nexts){info nextinfo = process(a);yes += nextinfo.no;no += max(nextinfo.yes, nextinfo.no);}return info(yes, no);
}
int maxhappy(employee* head)
{info allinfo = process(head);return max(allinfo.no, allinfo.yes);
}

5给定一个由字符串组成的数组strs,必须把所有的字符串拼接起来,返回所有可能的拼接结果中字典序最小的结果

贪心:局部最小得全体最优解,有时候可能会有错

 字典序:字符串排大小,长度一样比数字大小

长度不同:较短的补上最小的阿斯克码值,然后与长的比较

证明过程:得先证明排序过程具有传递性 ,像石头剪刀布就没有传递性

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class compare {
public:bool operator()(string a, string b){return (a + b) < (b + a);}
};
string lowestString(vector<string> str)
{if (str.empty())return "";sort(str.begin(), str.end(), compare());string a="";for (string c : str){a += c;}return a;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/736012.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

转移表回调函数实现

回调函数实现 计算器的模拟&#xff08;函数指针数组的使用&#xff09;&#xff08;回调函数&#xff09; 简化 冗余 老的代码的问题就是 冗余 写死 不能完成不同的任务 函数调用的时候只需要知道地址就可以 calc计算器 这里也称之为转移表 #define _CRT_SECURE_NO_WAR…

出现“error: failed to push some refs to ‘https://github.com/****.git‘”,如何解决问题

一、出错情况&#xff1a; 今天继续推送整理的知识点的时候&#xff0c;出现了一个报错。“error: failed to push some refs to https://github.com/.git”&#xff0c;百思不得其解&#xff0c;之前推送的时候都可以轻松推送成功&#xff0c;如今却说本地库与远程库不一致。…

腾讯云轻量应用服务器“月流量”什么意思?用完了怎么办?

腾讯云轻量应用服务器“月流量”什么意思&#xff1f;就是限制月流量的意思。腾讯云轻量服务器流量用完了怎么办&#xff1f;超额部分的流量另外支付流量费&#xff0c;流量价格为0.8元/GB&#xff0c;会自动扣你的腾讯云余额&#xff0c;如果你的腾讯云账号余额不足&#xff0…

Git win与linux换行符转换的问题

转载&#xff1a;warning: in the working copy of ‘package-lock.json‘, LF will be replaced by CRLF the next time Git_warning: in the working copy of package.json, lf-CSDN博客 warning: in the working copy of ‘package-lock.json‘, LF will be replaced by CRL…

ModStartBlog 稳定版 v9.0.0

ModStart 是一个基于 Laravel 模块化极速开发框架。模块市场拥有丰富的功能应用&#xff0c;支持后台一键快速安装&#xff0c;让开发者能快的实现业务功能开发。系统完全开源&#xff0c;基于 Apache 2.0 开源协议。 功能特性 丰富的模块市场&#xff0c;后台一键快速安装 会…

C++ Function Templates (函数模板)

C Function Templates [函数模板] 1. Templates and Generic Programming (模板与泛型编程)2. Defining a Function Templates (定义函数模板)2.1. Instantiating a Function Template (实例化函数模板)2.2. Template Type Parameters (模板类型参数)2.3. Nontype Template Par…

【实战项目】Boost搜索引擎项目

目录 1. 项目的相关背景 2. 搜索引擎的相关宏观原理 3. 搜索引擎技术栈和项目环境 4. 正排索引 vs 倒排索引 - 搜索引擎具体原理 4.1 正排索引 4.2 目标文档进行分词 4.3 倒排索引 4.4 模拟一次查找的过程&#xff1a; 5. 编写数据去标签与数据清洗的模块 Parser 5.1…

《操作系统真相还原》读书笔记八:获取物理内存容量以及本书源代码

编写mbr.S汇编代码 ;LOADER_BASE_ADDR equ 0x900 ;LOADER_START_SECTOR equ 0x2 %include "boot.inc"SECTION MBR vstart0x7c00mov ax,csmov ds,axmov es,axmov ss,axmov fs,axmov sp,0x7c00mov ax,0xb800mov gs,axmov ax,0x0600mov bx,0x0700mov cx,0mov dx, 184fh…

力扣图论篇

以下思路来自代码随想录以及官方题解。 文章目录 797.所有可能的路径200.岛屿数量130.被围绕的区域1020.飞地的数量 797.所有可能的路径 给你一个有 n 个节点的 有向无环图&#xff08;DAG&#xff09;&#xff0c;请你找出所有从节点 0 到节点 n-1 的路径并输出&#xff08;不…

USB协议学习(三)大容量存储设备SCSI协议分析

笔者来简单介绍一下SCSI得协议命令 1、SCSI协议认识 SCSI&#xff1a;Small Computer System Interface&#xff0c;用于计算机外部设备得接口标准&#xff0c;定义了与外部设备得一套协议。SCSI标准协议族支持很多钟SCSI设备&#xff0c;像盘&#xff0c;打印机&#xff0c;扫…

Ubuntu23.10安装FFmpeg及编译FFmpeg源码

安装FFmpeg: 打开终端: 输入 sudo apt install ffmpeg 安装成功: 验证FFmpeg 默认安装位置与库与头文件位置 使用FFmpeg源码编译: 1.安装YASM sudo apt-get install yasm

[mmucache]-ARMV8-aarch64的虚拟内存(mmutlbcache)介绍-概念扫盲

&#x1f525;博客主页&#xff1a; 小羊失眠啦. &#x1f3a5;系列专栏&#xff1a;《C语言》 《数据结构》 《C》 《Linux》 《Cpolar》 ❤️感谢大家点赞&#x1f44d;收藏⭐评论✍️ 思考: 1、cache的entry里都是有什么&#xff1f; 2、TLB的entry里都是有什么? 3、MMU操作…

10-ARM gicv3/gicv4的总结-基础篇

目录 1、gic的版本2、GICv3/gicv4的模型图3、gic中断号的划分4、GIC连接方式5、gic的状态6、gic框架7、gic Configuring推荐 本文转自 周贺贺&#xff0c;baron&#xff0c;代码改变世界ctw&#xff0c;Arm精选&#xff0c; armv8/armv9&#xff0c;trustzone/tee&#xff0c;s…

遗传算法(GA)求解基于栅格地图的机器人最优路径规划,可以自行修改地图(提供MATLAB代码)

通过栅格法建立栅格地图作为机器人路径规划的工作环境,采用遗传算法作为机器人路径搜索的规则.将所有机器人放置于初始位置.经过NC次无碰撞迭代运动找到最优路径.到达目标位置.为防止机器人在路径搜索过程中没有达到最大迭代次数时路径大小已不发生变化而陷入局部最优。 一、部…

JavaWeb-Maven基础

Maven是专门用于管理和构建Java项目的工具&#xff0c;是 Apache 下的一个纯 Java 开发的开源项目&#xff0c;基于项目对象模型&#xff08;POM&#xff09;概念。先来学习一下Maven基础&#xff0c;等后面学完开发框架后再学Maven高级&#xff0c;这次的内容如下 一、概述 …

Linux学习——线程的控制

目录 ​编辑 一&#xff0c;线程的创建 二&#xff0c;线程的退出 1&#xff0c;在子线程内return 2,使用pthread_exit(void*) 三&#xff0c;线程等待 四&#xff0c;线程获取自己的id值 五&#xff0c;线程取消 六&#xff0c;线程分离 一&#xff0c;线程的创建 在对…

企微hook源码

企微hook源码已经在QQ群内开源。速度进群下载&#xff0c;避免和谐。 QQ群&#xff1a;649480745 //pc端发送消息回调 void RecvPcMsgRes(DWORD eax) { // OutputDebugStringA("pc发送消息"); PWX_STRING pMsgPb (PWX_STRING)(eax);//消息pb结构体 string pbDa…

打卡--MySQL8.0 一(单机部署)

一路走来&#xff0c;所有遇到的人&#xff0c;帮助过我的、伤害过我的都是朋友&#xff0c;没有一个是敌人。如有侵权&#xff0c;请留言&#xff0c;我及时删除&#xff01; MySQL 8.0 简介 MySQL 8.0与5.7的区别主要体现在&#xff1a;1、性能提升&#xff1b;2、新的默认…

鸿蒙开发学习入门教程之环境配置

最近鸿蒙开发越来越火&#xff0c;各个大厂都有鸿蒙版本的计划和宣传&#xff0c;看这个趋势&#xff0c;可能会在几年内发展壮大&#xff0c;为我们移动端码农开辟一片新的职场。所以现在开始学起来还是很有必要的。今天就一起开始配置环境搞起来吧。 首先&#xff0c;找到官…

用户管理【MySQL】

文章目录 查看用户信息创建用户修改密码删除用户授予权限收回权限 查看用户信息 在名为mysql的数据库中有一个表user维护着 MySQL 的用户信息。 其中&#xff1a; user&#xff1a; 表示该用户的用户名。host&#xff1a; 表示该用户可以从哪个主机登录&#xff0c;localhost…