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

边栏推荐
- Oracle概念二
- 06. First introduction to express
- C language teacher workload management system
- 数组表示若干个区间的集合,请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。【LeetCodeHot100】
- 鴻蒙發力!HDD杭州站·線下沙龍邀您共建生態
- Ten common methods of arrays tools
- Taishan Office Technology Lecture: the first difficulty is vertical positioning
- 基于 Nebula Graph 构建百亿关系知识图谱实践
- #yyds干货盘点# 解决剑指offer:二叉树中和为某一值的路径(三)
- Event listening mechanism
猜你喜欢

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

Oracle concept II

Mihayou sued Minmetals trust, which was exposed to product thunderstorms

Yyds dry inventory solution sword finger offer: a path with a certain value in the binary tree (3)

全面解析零知识证明:消解扩容难题 重新定义「隐私安全」

After the mobile phone, it was reported that Samsung also cut the output of TV and other home appliance product lines

Sliding window + monotone queue concept and example (p1886 Logu)

Leetcode daily practice (sum of two numbers)

Etcd visualization tool: kstone deployment (I), rapid deployment based on Helm

Unity shadow shadow pancaking
随机推荐
Unity 阴影——阴影平坠(Shadow pancaking)
Mobile terminal click penetration
d3dx9_ How to repair 32.dll? d3dx9_ Solution to 32.dll missing
防火墙基础之源NAT地址转换和服务器映射web页面配置
字节跳动埋点数据流建设与治理实践
The two trump brand products of Langjiu are resonating in Chengdu, continuously driving the consumption wave of bottled liquor
等保2.0密码要求是什么?法律依据有哪些?
智慧风电 | 图扑软件数字孪生风机设备,3D 可视化智能运维
C language teacher workload management system
Domain name binding dynamic IP best practices
全面解析零知识证明:消解扩容难题 重新定义「隐私安全」
Leetcode daily practice (main elements)
IDE Eval reset unlimited trial reset
Alibaba cloud liupeizi: Inspiration from cloud games - innovation on the end
tensorflow求解泊松方程
Redis Series 2: data persistence improves availability
Pragma once Usage Summary
What do fast fashion brands care more about?
印象深刻的问题
The time of localdatetime type (2019-11-19t15:16:17) is queried with the time range of Oracle