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

边栏推荐
- About MySQL: the phenomenon and background of the problem
- Pragma once Usage Summary
- [multithreading] thread communication scheduling, waiting set wait(), notify()
- 智慧风电 | 图扑软件数字孪生风机设备,3D 可视化智能运维
- What is RPC
- 字节跳动埋点数据流建设与治理实践
- What are the password requirements for waiting insurance 2.0? What are the legal bases?
- Yyds dry inventory solution sword finger offer: a path with a certain value in the binary tree (3)
- Ten common methods of arrays tools
- Leetcode 33. Search rotation sort array
猜你喜欢

Autodesk NavisWorks 2022 software installation package download and installation tutorial

Leetcode 5. Longest Palindromic Substring

leetcode 200. Number of islands

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

当发布/订阅模式遇上.NET

Annual comprehensive analysis of China's audio market in 2022

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

Open source 23 things shardingsphere and database mesh have to say

Leetcode 33. Search rotation sort array

Cesium realizes satellite orbit detour
随机推荐
[the way of programmer training] - 3 Character count statistics
锚文本大量丢失的问题
Relation and operation of ORM table
Simulated process scheduling
#yyds干货盘点# 解决剑指offer:二叉树中和为某一值的路径(三)
LeetCode 124. Binary tree maximum path sum - binary tree series question 8
When the publish / subscribe mode encounters NET
黑马程序员-软件测试基础班-02-30-45工具代开浏览器运行代码,音、视频、测试点,音视频标签,布局标签。超链接语法进阶,绝对路径,相对路径
Detailed explanation of various GPIO input and output modes (push-pull, open drain, quasi bidirectional port)
Byte + Google super full kotlin learning King fried notes! Kotlin introductory tutorial + Advanced kotlin enhanced actual combat (with demo)
Autodesk Navisworks 2022软件安装包下载及安装教程
d3dx9_ What if 27.dll is lost? d3dx9_ How to repair 27.dll?
Why should string be designed to be immutable?
Oracle concept II
C语言集合运算
Special function calculator
tensorflow求解泊松方程
Leetcode 33. Search rotation sort array
How to modify / display GPIO status through ADB shell
[fxcg] today's market analysis