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

20240312-算法复习打卡day21||● 530.二叉搜索树的最小绝对差 ● 501.二叉搜索树中的众数 ● 236. 二叉树的最近公共祖先

  •  530.二叉搜索树的最小绝对差 

1.中序遍历得到升序数组

class Solution {
private:vector<int> vec;void traversal(TreeNode* root) {if (root == NULL) return;if (root->left) traversal(root->left);vec.push_back(root->val);if (root->right) traversal(root->right);}
public:int getMinimumDifference(TreeNode* root) {vec.clear();traversal(root);if (vec.size() < 2) return 0;int result = INT_MAX;for (int i = 1; i < vec.size(); i++) {result = min(result, vec[i] - vec[i - 1]);}return result;}
};

2.递归的时候进行最小绝对差的比较

class Solution {
private:int result = INT_MAX;TreeNode* pre = NULL;void traversal(TreeNode* cur) {if (cur == NULL) return;traversal(cur->left);if (pre != NULL) {result = min(result, cur->val - pre->val);}pre = cur;traversal(cur->right);}
public:int getMinimumDifference(TreeNode* root) {traversal(root);return result;}
};
  •  501.二叉搜索树中的众数 

1.二叉树情况

class Solution {
private:void searchBST(TreeNode* cur, unordered_map<int, int>& map) {if (cur == NULL) return;map[cur->val]++;searchBST(cur->left, map);searchBST(cur->right, map);return;}bool static cmp (const pair<int ,int>& a, const pair<int, int>& b) {return a.second > b.second;}
public:vector<int> findMode(TreeNode* root) {unordered_map<int, int> map;vector<int> result;if (root == NULL) return result;searchBST(root, map);vector<pair<int, int>> vec(map.begin(), map.end());sort(vec.begin(), vec.end(), cmp);result.push_back(vec[0].first);for (int i = 1; i < vec.size(); i++) {if (vec[i].second == vec[0].second) result.push_back(vec[i].first);else break;}return result;}
};

2.搜索二叉树情况

class Solution {
private:int maxCount = 0;int count = 0;TreeNode* pre = NULL;vector<int> result;void searchBST(TreeNode* cur) {if (cur == NULL) return;searchBST(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);}searchBST(cur->right);return;}
public:vector<int> findMode(TreeNode* root) {count = 0;maxCount = 0;pre = NULL;result.clear();searchBST(root);return result;}
};

  •  236. 二叉树的最近公共祖先  

1.节点为p或者q或者NULL的时候,返回本身的值到上一个节点去;

2.只有左右都不为NULL(找到了公共祖先),把此时的root节点返回上去(返回上去的只有p,q,NULL,公共祖先这四种情况)

class Solution {
public:TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {if (root == q || root == p || root == NULL) return root;TreeNode* left = lowestCommonAncestor(root->left, p, q);TreeNode* right = lowestCommonAncestor(root->right, p, q);if (left != NULL && right != NULL) return root;if (left != NULL && right == NULL) return left;else if (left == NULL && right != NULL) return right;else {return NULL;}}
};

http://www.lryc.cn/news/318406.html

相关文章:

  • 今天我们来学习一下关于MySQL数据库
  • 长期护理保险可改善老年人心理健康 | CHARLS CLHLS CFPS 公共数据库周报(3.6)...
  • 49、C++/友元、常成员函数和常对象、运算符重载学习20240314
  • SQL Server错误:15404
  • Halcon文件操作
  • 【测试知识】业务面试问答突击版1
  • 使用el-row及el-col页面缩放时出现空行解决方案
  • java中几种对象存储(文件存储)中间件的介绍
  • 网络工程师——2024自学
  • SwiftUI的Picker
  • 物联网技术助力智慧城市转型升级:智能、高效、可持续
  • YOLOv7_pose-Openvino和ONNXRuntime推理【CPU】
  • 通过ACPI检测沙箱-反虚拟机
  • 计算点集的最小外接矩形——OpenCV的minAreaRect函数
  • Stripe Web 购买集成
  • 加密货币在网络违法犯罪活动中的利用情况调查
  • 【测试知识】业务面试问答突击版3---bug、测试用例设计
  • 使用大型语言模型进行实体提取
  • 基础:TCP是什么?
  • el-table中 el-popover 性能优化
  • java数据结构与算法刷题-----LeetCode46. 全排列
  • 听说过Nginx反向代理,那正向代理是什么?
  • 实现elasticsearch和数据库的数据同步
  • SwiftUI的Alert使用方式
  • FPGA高端项目:FPGA基于GS2971的SDI视频接收+GTX 8b/10b编解码SFP光口传输,提供2套工程源码和技术支持
  • 【源码编译】Apache SeaTunnel-Web 适配最新2.3.4版本教程
  • 数据集下载
  • 3、设计模式之工厂模式2(Factory)
  • npm、nodejs和vue之间关系和区别介绍
  • DM数据库安装(Windows)