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

静海网站建设快速提升关键词排名软件

静海网站建设,快速提升关键词排名软件,小程序开发文档说明,做网站引流的利润给你一个含重复值的二叉搜索树(BST)的根节点 root ,找出并返回 BST 中的所有 众数(即,出现频率最高的元素)。 如果树中有不止一个众数,可以按 任意顺序 返回。 假定 BST 满足如下定义&#xf…

给你一个含重复值的二叉搜索树(BST)的根节点 root ,找出并返回 BST 中的所有 众数(即,出现频率最高的元素)。

如果树中有不止一个众数,可以按 任意顺序 返回。

假定 BST 满足如下定义:

  • 结点左子树中所含节点的值 小于等于 当前节点的值
  • 结点右子树中所含节点的值 大于等于 当前节点的值
  • 左子树和右子树都是二叉搜索树

sample
提示:

树中节点的数目在范围 [1, 104]

  • 105 <= Node.val <= 105

进阶:你可以不使用额外的空间吗?(假设由递归产生的隐式调用栈的开销不被计算在内)

AC:

/** @lc app=leetcode.cn id=501 lang=cpp** [501] 二叉搜索树中的众数*/// @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:TreeNode* pre = NULL;int count = 0;int maxCount = 0;vector<int> result;void traversal(TreeNode* cur) {if(cur == NULL)return ;traversal(cur->left);if(pre == NULL)count = 1;else if(pre->val == cur->val){count++;}else count = 1;pre = cur;if(count == maxCount){result.push_back(cur->val);}if(count > maxCount){maxCount = count;result.clear();result.push_back(cur->val);}traversal(cur->right);return ;}vector<int> findMode(TreeNode* root) {traversal(root);return result;}
};
// @lc code=end

AC
Tips:

  • 对于一次遍历完搜索二叉树,将众数结果统计出来:
		if(count == maxCount){result.push_back(cur->val);}if(count > maxCount){maxCount = count;result.clear();result.push_back(cur->val);}

The first if statement checks if the count of the current value is equal to the maximum count seen so far (count == maxCount). If the count is equal to the maximum count, then the current value is also a mode of the binary search tree, so the value is added to the result vector using the push_back function.

The second if statement checks if the count of the current value is greater than the maximum count seen so far (count > maxCount). If the count is greater than the maximum count, then the current value is a new mode of the binary search tree, so the result vector is cleared using the clear function, and the current value is added to the result vector using the push_back function. Additionally, the maxCount variable is updated to reflect the new maximum count.

Overall, this block of code is a simple and efficient way to update the result vector with the mode(s) of a binary search tree. The code uses a straightforward approach to keep track of the count of each value in the binary search tree, and updates the result vector whenever a new mode is found. One possible way to improve the code would be to add error checking to ensure that cur is not a null pointer before accessing its value. Additionally, the variable names could be more descriptive to make the code easier to read and understand.


  • 二叉树双指针的移动:
		pre = cur;

The line of code pre = cur; is used to update the pre pointer to point to the current node cur. This is because the function is traversing the binary search tree in order, and pre needs to point to the previous node in order to calculate the count of each value in the binary search tree.

Specifically, the function uses an in-order traversal of the binary search tree to visit each node in ascending order. For each node, the function calculates the count of the node’s value by comparing it to the value of the previous node. If the value is the same as the previous node’s value, then the count is incremented. Otherwise, the count is reset to 1.

The pre pointer is used to keep track of the previous node visited during the in-order traversal. Initially, pre is set to nullptr to indicate that there is no previous node. For each node cur, the line of code pre = cur; updates pre to point to cur, so that pre will point to the previous node during the next iteration of the loop.

Overall, this line of code is a simple and efficient way to update the pre pointer during an in-order traversal of a binary search tree. One possible way to improve the code would be to add error checking to ensure that pre and cur are not null pointers before updating pre. Additionally, the variable names could be more descriptive to make the code easier to read and understand.

http://www.yayakq.cn/news/32193/

相关文章:

  • 网站做外链网站公告模板代码
  • 网站建设课程培训ui设计方向网站建设目标
  • 广告标识标牌制作公司seo研究中心怎么了
  • 建一个网站大约需要花费多少钱社交网站开发技术岗
  • 四川网站建设 lkcms乔拓云智能建站免费注册
  • 网站建设国家标准自己的网站没有域名
  • 梅州哪里做网站想用wordpress建立网站
  • 公司网站建设站酷通过信息系统融合和创新形成企业解决方案
  • 做医疗设备的网站网站开发技术总结
  • 中国银行全球门户网站wordpress侧边栏折叠菜单
  • 南昌网站建设平台校园电子商务是什么
  • 旅行社网站 模板科技馆里面有什么展品
  • 三门网站制作河南手机网站设计
  • 帝国cms 制作网站地图网页广告
  • 域名做网站wap是什么东西
  • 网站建设 服务承诺网页制作软件中的三剑客
  • 做网站设计师工资多少广州市网站建设科技
  • 网站ui设计模板全网vip影视自助建站系统
  • 建设银行信用卡被钓鱼网站骗了15000青岛logo设计价格
  • 长沙医考网站建设公司邮政管理网站建设
  • 佛山大型网站建设怎么创建小程序卖东西
  • 牡丹江建设局网站百度seo优化收费标准
  • 图书馆馆建设网站江西省企业登记网络服务平台官网
  • 推广优化网站做国学类网站合法吗
  • 建设银行网站机构网页美工设计岗前培训
  • 国家开发银行生源地助学贷款网站app专业定制开发
  • 怎么做加密货币网站公众号涨粉平台
  • 昆山高端网站设计公司工程建设数字化管理平台
  • 重庆建设工程信息网官网app更换模板对网站seo的影响
  • 织梦可以做移动网站吗代发新闻稿的网站