当前位置:网站首页>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;
}
};

边栏推荐
- 郎酒两大王牌产品成都联动共振,持续带动光瓶酒消费浪潮
- Popularization of MCU IO port: detailed explanation of push-pull output and open drain output
- New method of cross domain image measurement style relevance: paper interpretation and code practice
- LeetCode 124. Binary tree maximum path sum - binary tree series question 8
- 米哈游起诉五矿信托,后者曾被曝产品暴雷
- Raspberry pie preliminary use
- Use pyinstaller to package py files into exe. Precautions and error typeerror:_ get_ sysconfigdata_ name() missing 1...‘ check_ Solutions to exists'
- QT5 之信号与槽机制(信号与槽的基本介绍)
- Kubernetes basic self-study series | introduction to ingress API
- C語言教師工作量管理系統
猜你喜欢

The two trump brand products of Langjiu are resonating in Chengdu, continuously driving the consumption wave of bottled liquor
![[fxcg] today's market analysis](/img/97/fc6faa5ab693e383f085b407ce1d85.jpg)
[fxcg] today's market analysis

EMQ 助力青岛研博建设智慧水务平台

07. Express routing

d3dx9_ How to repair 25.dll? d3dx9_ 25.dll where to download

Cesium realizes satellite orbit detour

A robot is located in the upper left corner of an M x n grid. The robot can only move down or right one step at a time. The robot attempts to reach the lower right corner of the grid. How many differe

一个机器人位于一个 m x n 网格的左上角 。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角。问总共有多少条不同的路径?【LeetCodeHot100】

Construction and management practice of ByteDance buried point data flow

IDE Eval reset unlimited trial reset
随机推荐
[the way of programmer training] - 3 Character count statistics
Mihayou sued Minmetals trust, which was exposed to product thunderstorms
ROS "topic" programming implementation
Oracle概念三
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
国家食品安全风险评估中心:不要盲目片面追捧标签为“零添加”“纯天然”食品
2/14 preliminary calculation geometry
d3dx9_ How to repair 32.dll? d3dx9_ Solution to 32.dll missing
郎酒两大王牌产品成都联动共振,持续带动光瓶酒消费浪潮
09 route guard authenticates URL
07. Express routing
Bit. Store: long bear market, stable stacking products may become the main theme
What are the password requirements for waiting insurance 2.0? What are the legal bases?
数据中心表格报表实现定制统计加班请假汇总记录分享
Why should string be designed to be immutable?
IDE Eval reset unlimited trial reset
Simulated process scheduling
Hierarchical clustering and case analysis
Hongmeng makes efforts! HDD Hangzhou station · offline salon invites you to build ecology
d3dx9_ How to repair 40.dll? Win10 system d3dx9_ What if 40.dll is lost?