当前位置:网站首页>428-二叉树(501.二叉搜索树中的众数、701.二叉搜索树中的插入操作、450.删除二叉搜索树中的节点、669. 修剪二叉搜索树)
428-二叉树(501.二叉搜索树中的众数、701.二叉搜索树中的插入操作、450.删除二叉搜索树中的节点、669. 修剪二叉搜索树)
2022-06-27 15:52:00 【liufeng2023】
501. 二叉搜索树中的众数
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. 二叉搜索树中的插入操作
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.删除二叉搜索树中的节点
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. 修剪二叉搜索树
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;
}
};
边栏推荐
- Defiato is an innovation that combines user-friendly features of a centralized platform with defi services
- Data center table reports realize customized statistics, overtime leave summary record sharing
- The time of localdatetime type (2019-11-19t15:16:17) is queried with the time range of Oracle
- Bit. Store: long bear market, stable stacking products may become the main theme
- 当发布/订阅模式遇上.NET
- What is RPC
- Pragma once Usage Summary
- P.A.R.A 方法在思源的简易应用(亲测好用)
- 【牛客刷题】NowCoder号称自己已经记住了1-100000之间所有的斐波那契数。 为了考验他,我们随便出一个数n,让他说出第n个斐波那契数。如果第n个斐波那契大于6位则只取后6位。
- Logstash excludes specific files or folders from collecting report log data
猜你喜欢
数组表示若干个区间的集合,请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。【LeetCodeHot100】
关于#mysql#的问题:问题遇到的现象和发生背景
When the publish / subscribe mode encounters NET
Bit. Store: long bear market, stable stacking products may become the main theme
d3dx9_ Where is 35.dll? d3dx9_ Where can I download 35.dll
P. Simple application of a.r.a method in Siyuan (friendly testing)
Hung - Mung! HDD Hangzhou station · salon hors ligne vous invite à construire l'écologie
LeetCode 124. Binary tree maximum path sum - binary tree series question 8
Source NAT address translation and server mapping web page configuration of firewall Foundation
Etcd visualization tool: kstone deployment (I), rapid deployment based on Helm
随机推荐
Sliding window + monotone queue concept and example (p1886 Logu)
Use pyinstaller to package py files into exe. Precautions and error typeerror:_ get_ sysconfigdata_ name() missing 1...‘ check_ Solutions to exists'
2022年中国音频市场年度综合分析
Pragma once Usage Summary
Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance
The European unified charging specification act was passed before the end of the year, and it is planned to expand to products such as laptop and keyboard
米哈游起诉五矿信托,后者曾被曝产品暴雷
Simulated process scheduling
树莓派初步使用
Drawing for example study of flashcc
模拟进程调度
d3dx9_ How to repair 40.dll? Win10 system d3dx9_ What if 40.dll is lost?
National food safety risk assessment center: do not blindly and unilaterally pursue "zero addition" and "pure natural" food
d3dx9_ How to repair 33.dll? d3dx9_ What if 33.dll is lost?
Impressive questions
[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
Handwritten promise series - all
阿里云刘珅孜:云游戏带来的启发——端上创新
锚文本大量丢失的问题
Halcon: discrete digital OCR recognition