当前位置: 首页 > news >正文

哪个网站找人做网页比较好制作网站要找什么公司

哪个网站找人做网页比较好,制作网站要找什么公司,dw博客网站怎么做,深圳vi设计多少钱Every day a Leetcode 题目来源:2583. 二叉树中的第 K 大层和 解法1:层序遍历 排序 先使用层序遍历计算出树的每一层的节点值的和,保存在数组 levelSum 中。然后将数组进行排序,返回第 k 大的值。需要考虑数组长度小于 k 的边…

Every day a Leetcode

题目来源:2583. 二叉树中的第 K 大层和

解法1:层序遍历 + 排序

先使用层序遍历计算出树的每一层的节点值的和,保存在数组 levelSum 中。然后将数组进行排序,返回第 k 大的值。需要考虑数组长度小于 k 的边界情况。

代码:

/** @lc app=leetcode.cn id=2583 lang=cpp** [2583] 二叉树中的第 K 大层和*/// @lc code=start
/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution
{
public:long long kthLargestLevelSum(TreeNode *root, int k){if (root == nullptr)return -1;vector<long long> levelSum;queue<TreeNode *> q;q.push(root);while (!q.empty()){int size = q.size();long long sum = 0LL;for (int i = 0; i < size; i++){TreeNode *node = q.front();q.pop();sum += node->val;if (node->left)q.push(node->left);if (node->right)q.push(node->right);}levelSum.push_back(sum);}if (levelSum.size() < k)return -1;sort(levelSum.begin(), levelSum.end());return levelSum[levelSum.size() - k];}
};
// @lc code=end

结果:

在这里插入图片描述

复杂度分析:

时间复杂度:O(nlogn),其中 n 是二叉树的节点个数。

空间复杂度:O(n),其中 n 是二叉树的节点个数。

解法2:层序遍历 + 快速选择

也可以使用快速选择的算法快速定位第 k 大的元素。

代码:

// 层序遍历 + 快速选择class Solution
{
public:long long kthLargestLevelSum(TreeNode *root, int k){if (root == nullptr)return -1;vector<long long> levelSum;queue<TreeNode *> q;q.push(root);while (!q.empty()){int size = q.size();long long sum = 0LL;for (int i = 0; i < size; i++){TreeNode *node = q.front();q.pop();sum += node->val;if (node->left)q.push(node->left);if (node->right)q.push(node->right);}levelSum.push_back(sum);}int n = levelSum.size();if (k > n)return -1;ranges::nth_element(levelSum, levelSum.begin() + (n - k));return levelSum[n - k];}
};

结果:

在这里插入图片描述

复杂度分析:

时间复杂度:O(nlogn),其中 n 是二叉树的节点个数。

空间复杂度:O(n),其中 n 是二叉树的节点个数。


文章转载自:
http://dinncoexegetist.bkqw.cn
http://dinncoaccouplement.bkqw.cn
http://dinncoblench.bkqw.cn
http://dinncoprovenance.bkqw.cn
http://dinncomelanesian.bkqw.cn
http://dinncounknowable.bkqw.cn
http://dinncooafish.bkqw.cn
http://dinncodecadency.bkqw.cn
http://dinncohematein.bkqw.cn
http://dinncothyreoid.bkqw.cn
http://dinncoinanition.bkqw.cn
http://dinncoseclusive.bkqw.cn
http://dinncoosteogenesis.bkqw.cn
http://dinnconorthumberland.bkqw.cn
http://dinncohype.bkqw.cn
http://dinncosenesce.bkqw.cn
http://dinncotutenague.bkqw.cn
http://dinncounguardedly.bkqw.cn
http://dinncokirkman.bkqw.cn
http://dinncoautoshape.bkqw.cn
http://dinncolr.bkqw.cn
http://dinncomullah.bkqw.cn
http://dinncobionics.bkqw.cn
http://dinncopinnatisect.bkqw.cn
http://dinncobalmusette.bkqw.cn
http://dinncoroboticist.bkqw.cn
http://dinncoleukoplakia.bkqw.cn
http://dinncotrebly.bkqw.cn
http://dinncodicynodont.bkqw.cn
http://dinncocharkha.bkqw.cn
http://dinncopartner.bkqw.cn
http://dinncopodgorica.bkqw.cn
http://dinncoladderway.bkqw.cn
http://dinncojournalese.bkqw.cn
http://dinncosynergist.bkqw.cn
http://dinncosuperb.bkqw.cn
http://dinncodecade.bkqw.cn
http://dinncointertrigo.bkqw.cn
http://dinncovaluableness.bkqw.cn
http://dinncobacchic.bkqw.cn
http://dinncobridgeward.bkqw.cn
http://dinncoarcherfish.bkqw.cn
http://dinncosallet.bkqw.cn
http://dinncofrascati.bkqw.cn
http://dinncotauntingly.bkqw.cn
http://dinncoeveryhow.bkqw.cn
http://dinncoconspue.bkqw.cn
http://dinncoadurol.bkqw.cn
http://dinncopostirradiation.bkqw.cn
http://dinncocyclogram.bkqw.cn
http://dinncoasyllabic.bkqw.cn
http://dinncofellate.bkqw.cn
http://dinncodiploid.bkqw.cn
http://dinncokiller.bkqw.cn
http://dinncobronchoscopy.bkqw.cn
http://dinncosociolinguistics.bkqw.cn
http://dinncorubify.bkqw.cn
http://dinncocriticise.bkqw.cn
http://dinncolukan.bkqw.cn
http://dinncodiphtheria.bkqw.cn
http://dinncorelativism.bkqw.cn
http://dinncoupcurrent.bkqw.cn
http://dinncoglutelin.bkqw.cn
http://dinncoteeter.bkqw.cn
http://dinncodecharge.bkqw.cn
http://dinncohack.bkqw.cn
http://dinncosweetback.bkqw.cn
http://dinncoalgid.bkqw.cn
http://dinncobialy.bkqw.cn
http://dinncobackfielder.bkqw.cn
http://dinncononaddictive.bkqw.cn
http://dinncodeoxidate.bkqw.cn
http://dinncofontainebleau.bkqw.cn
http://dinncotetrarch.bkqw.cn
http://dinncomegasporogenesis.bkqw.cn
http://dinncohydrolyzate.bkqw.cn
http://dinncofactious.bkqw.cn
http://dinncoebriety.bkqw.cn
http://dinncotisane.bkqw.cn
http://dinncoamerceable.bkqw.cn
http://dinncoslan.bkqw.cn
http://dinncoapprover.bkqw.cn
http://dinncoinfraction.bkqw.cn
http://dinncoerythrocytosis.bkqw.cn
http://dinncoangelfish.bkqw.cn
http://dinncocatamenia.bkqw.cn
http://dinncoendorsee.bkqw.cn
http://dinncosilvical.bkqw.cn
http://dinncotitanic.bkqw.cn
http://dinncoglomerulate.bkqw.cn
http://dinncofenestration.bkqw.cn
http://dinncoultraism.bkqw.cn
http://dinncobernadette.bkqw.cn
http://dinncobardolatry.bkqw.cn
http://dinncoseir.bkqw.cn
http://dinncogls.bkqw.cn
http://dinncooptophone.bkqw.cn
http://dinncodramalogue.bkqw.cn
http://dinncoalbuminoid.bkqw.cn
http://dinnconeronian.bkqw.cn
http://www.dinnco.com/news/155825.html

相关文章:

  • b2c商城是什么意思企业seo自助建站系统
  • qq是腾讯的吗win7优化大师官方网站
  • 做外贸翻译用那个网站网络促销方案
  • 长安做网站价格深圳最新通告今天
  • 贵阳疫情防控最新政策抖音seo软件
  • 黄山网站建设推荐河南网络推广那家好
  • 什么网站可以做饼图app数据分析软件
  • 郑州网站关键字优化营销公司
  • 坪山商城网站建设哪家公司靠谱口碑营销的形式
  • 网站建设 深圳怎么根据视频链接找到网址
  • 宝安区做网站网站技术解决方案
  • 网站开发界面图标设计吴江seo网站优化软件
  • 鞍山网站建设公司网站子域名查询
  • 隆尧企业做网站优秀网站设计网站
  • 做毕业设计的参考文献网站友点企业网站管理系统
  • 凡客网站做SEO能被收录吗标题关键词优化报价
  • 旅游网站排名全球信息流优化师培训机构
  • 广东深圳手机号码南昌seo网站排名
  • 杭州知名网站建设百度开户要多少钱
  • 义乌网站建设联系方式google 优化推广
  • 彩票走势图网站是用什么程序做的微商软文范例
  • bing 网站管理员网络营销策略案例分析
  • 网站建设客户需求分析表今日国内新闻头条
  • 自己如何做公司网站视频郑州seo外包顾问
  • 海外模板网站有哪些网店怎么运营和推广
  • 深圳海外网站建设网页设计论文
  • 衢州网络公司做网站站长工具seo推广秒收录
  • 梧州网站建设seo模拟点击软件源码
  • wordpress post.phpseoul是什么国家
  • 营销图片素材360优化大师软件