当前位置:网站首页>428 binary tree (501. mode in binary search tree, 701. insert operation in binary search tree, 450. delete node in binary search tree, 669. prune binary search tree)
428 binary tree (501. mode in binary search tree, 701. insert operation in binary search tree, 450. delete node in binary search tree, 669. prune binary search tree)
2022-06-27 17:01:00 【liufeng2023】
501. The mode in the binary search tree
class Solution {
public:
void search_bst(TreeNode* cur, unordered_map<int, int>& mp)
{
if (cur == nullptr) return;
mp[cur->val]++;
search_bst(cur->left, mp);
search_bst(cur->right, mp);
}
public:
vector<int> findMode(TreeNode* root) {
unordered_map<int, int> mp;
vector<int> res;
if (root == nullptr) return res;
search_bst(root, mp);
vector<pair<int, int>> vec(mp.begin(), mp.end());
sort(vec.begin(), vec.end(),
[](const pair<int, int>& x, const pair<int, int>& y) -> bool
{
return x.second > y.second;
});
res.push_back(vec[0].first);
for (int i = 1; i < vec.size(); i++)
{
if (vec[i].second == vec[0].second) res.push_back(vec[i].first);
else break;
}
return res;
}
};
701. Insert operation in binary search tree
class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
if (root == nullptr)
{
TreeNode* node = new TreeNode(val);
return node;
}
if (root->val > val) root->left = insertIntoBST(root->left, val);
if (root->val < val) root->right = insertIntoBST(root->right, val);
return root;
}
};
450. Delete nodes in binary search tree
class Solution {
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if (root == nullptr) return root;
if (root->val == key)
{
if (root->left == nullptr && root->right == nullptr)
{
delete root;
return nullptr;
}
else if (root->left == nullptr)
{
TreeNode* retNode = root->right;
delete root;
return retNode;
}
else if (root->right == nullptr)
{
TreeNode* retNode = root->left;
delete root;
return retNode;
}
else
{
TreeNode* cur = root->right;
while (cur->left != nullptr)
{
cur = cur->left;
}
cur->left = root->left;
TreeNode* tmp = root;
root = root->right;
delete tmp;
return root;
}
}
if (root->val > key) root->left = deleteNode(root->left, key);
if (root->val < key) root->right = deleteNode(root->right, key);
return root;
}
};
669. Prune the binary search tree
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int low, int high) {
if (root == nullptr) return nullptr;
if (root->val < low)
{
TreeNode* right = trimBST(root->right, low, high);
return right;
}
if (root->val > high)
{
TreeNode* left = trimBST(root->left, low, high);
return left;
}
root->left = trimBST(root->left, low, high);
root->right = trimBST(root->right, low, high);
return root;
}
};
边栏推荐
- Array represents a collection of several intervals. Please merge all overlapping intervals and return a non overlapping interval array. The array must exactly cover all the intervals in the input. 【Le
- Alibaba cloud liupeizi: Inspiration from cloud games - innovation on the end
- 华为云DevCloud重磅发布四大新能力,创下国内两项第一
- 07. Express routing
- Detailed explanation of various GPIO input and output modes (push-pull, open drain, quasi bidirectional port)
- [fxcg] today's market analysis
- #yyds干货盘点# 解决剑指offer:二叉树中和为某一值的路径(三)
- Leetcode daily practice (main elements)
- 国家食品安全风险评估中心:不要盲目片面追捧标签为“零添加”“纯天然”食品
- QT5.5.1桌面版安装配置过程中的疑难杂症处理(配置ARM编译套件)
猜你喜欢
Autodesk Navisworks 2022软件安装包下载及安装教程
Defiato is an innovation that combines user-friendly features of a centralized platform with defi services
Synchronization mechanism of dual namenodes
Handling method of occasional error reporting on overseas equipment
Leetcode daily practice (sum of two numbers)
Hongmeng makes efforts! HDD Hangzhou station · offline salon invites you to build ecology
Drawing for example study of flashcc
[Niuke's questions] nowcoder claims to have remembered all Fibonacci numbers between 1 and 100000. To test him, we gave him a random number N and asked him to say the nth Fibonacci number. If the nth
What is the level 3 password complexity of ISO? How often is it replaced?
09 route guard authenticates URL
随机推荐
Bit. Store: long bear market, stable stacking products may become the main theme
Delete duplicate elements in the sorting linked list
事件监听机制
米哈游起诉五矿信托,后者曾被曝产品暴雷
鸿蒙发力!HDD杭州站·线下沙龙邀您共建生态
Determine the maximum number of specific words in a string
软件测试基础-软件测试历史流程,分类,好处,限制
d3dx9_ How to repair 25.dll? d3dx9_ 25.dll where to download
Etcd visualization tool: kstone deployment (I), rapid deployment based on Helm
关于VS2019C#如何建立登陆界面输入的用户名和密码需与Access数据库的记录相匹配
How to improve it electronic equipment performance management
List to table
Oracle概念三
数组表示若干个区间的集合,请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。【LeetCodeHot100】
d3dx9_ Where is 35.dll? d3dx9_ Where can I download 35.dll
QT5 之信号与槽机制(信号与槽的基本介绍)
Performance problems caused by redis cache invalidation and competitive loading
特殊函数计算器
Halcon: discrete digital OCR recognition
d3dx9_ What if 27.dll is lost? d3dx9_ How to repair 27.dll?