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

边栏推荐
- d3dx9_ How to repair 32.dll? d3dx9_ Solution to 32.dll missing
- The two trump brand products of Langjiu are resonating in Chengdu, continuously driving the consumption wave of bottled liquor
- #yyds干货盘点#简述chromeV8引擎垃圾回收
- After the mobile phone, it was reported that Samsung also cut the output of TV and other home appliance product lines
- Yyds dry inventory brief chrome V8 engine garbage collection
- 软件测试学习-黑马程序员,软件测试学习大纲
- 深耕数字化,引领云原生,服务更多开发者
- Under the influence of external factors, how to manage internal resources and reduce costs is the key
- C語言教師工作量管理系統
- 数据中心表格报表实现定制统计加班请假汇总记录分享
猜你喜欢

Leetcode daily practice (main elements)

Drawing for example study of flashcc

医院预约挂号系统-系统结构

Why should string be designed to be immutable?

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

继手机之后 报道称三星也削减了电视等家电产品线的产量

Popularization of MCU IO port: detailed explanation of push-pull output and open drain output

10 minutes to master the installation steps of MySQL

【多线程】线程通信调度、等待集 wait() 、notify()

软件测试学习-黑马程序员,软件测试学习大纲
随机推荐
P. Simple application of a.r.a method in Siyuan (friendly testing)
Etcd可视化工具:Kstone部署(一),基于Helm快速部署
Ten common methods of arrays tools
QT5 之信号与槽机制(演示控件自带的信号与槽函数关联)
鴻蒙發力!HDD杭州站·線下沙龍邀您共建生態
Julia constructs diagonal matrix
The array of C language is a parameter to pass a pointer
C語言教師工作量管理系統
How to improve it electronic equipment performance management
[the way of programmer training] - 3 Character count statistics
QT audio playback upgrade (7)
What are the password requirements for waiting insurance 2.0? What are the legal bases?
Cloud security daily 220216: root privilege escalation vulnerability found on IBM SaaS integration platform needs to be upgraded as soon as possible
Data center table reports realize customized statistics, overtime leave summary record sharing
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
IDE Eval reset unlimited trial reset
06. First introduction to express
Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance
国家食品安全风险评估中心:不要盲目片面追捧标签为“零添加”“纯天然”食品
SQLite net (SQLite is used by unity, WPF and WinForm)