当前位置:网站首页>429-二叉树(108. 将有序数组转换为二叉搜索树、538. 把二叉搜索树转换为累加树、 106.从中序与后序遍历序列构造二叉树、235. 二叉搜索树的最近公共祖先)
429-二叉树(108. 将有序数组转换为二叉搜索树、538. 把二叉搜索树转换为累加树、 106.从中序与后序遍历序列构造二叉树、235. 二叉搜索树的最近公共祖先)
2022-06-27 17:52:00 【liufeng2023】
108. 将有序数组转换为二叉搜索树

class Solution {
public:
TreeNode* traversal(vector<int>& nums, int left, int right)
{
if (left > right) return nullptr;
int mid = left + ((right - left) / 2);
TreeNode* root = new TreeNode(nums[mid]);
root->left = traversal(nums, left, mid - 1);
root->right = traversal(nums, mid + 1, right);
return root;
}
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
TreeNode* root = traversal(nums, 0, nums.size() - 1);
return root;
}
};

538. 把二叉搜索树转换为累加树

class Solution {
private:
int pre;
void traversal(TreeNode* cur)
{
if (cur == nullptr) return;
traversal(cur->right);
cur->val += pre;
pre = cur->val;
traversal(cur->right);
}
public:
TreeNode* convertBST(TreeNode* root) {
pre = 0;
traversal(root);
return root;
}
};

106.从中序与后序遍历序列构造二叉树

class Solution {
public:
TreeNode* traversal(vector<int>& inorder, vector<int>& postorder) {
if (postorder.size() == 0) return nullptr;
//后序遍历数组最后一个元素,就当当前的中间节点
int rootValue = postorder[postorder.size() - 1];
TreeNode* root = new TreeNode(rootValue);
//叶子节点
if (postorder.size() == 1) return root;
// 找到中序遍历的切割点
int del;
for (del = 0; del < inorder.size(); del++)
{
if (inorder[del] == rootValue) break;
}
// 切割中序数组
// 左闭右开区间:[0, delimiterIndex)
vector<int> leftInorder(inorder.begin(), inorder.begin() + del);
vector<int> rightInorder(inorder.begin() + del + 1, inorder.end());
// postorder 舍弃末尾元素
postorder.resize(postorder.size() - 1);
//切割后续数组
vector<int> leftPostorder(postorder.begin(), postorder.begin() + leftInorder.size());
vector<int> rightPostorder(postorder.begin() + leftInorder.size(), postorder.end());
root->left = traversal(leftInorder, leftPostorder);
root->right = traversal(rightInorder, rightPostorder);
return root;
}
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder)
{
if (inorder.size() == 0 || postorder.size() == 0) return nullptr;
return traversal(inorder, postorder);
}
};

235. 二叉搜索树的最近公共祖先

class Solution {
public:
TreeNode* traversal(TreeNode* cur, TreeNode* p, TreeNode* q) {
if (cur == nullptr) return cur;
if (cur->val > p->val && cur->val > q->val)
{
TreeNode* left = traversal(cur->left, p, q);
if (left != nullptr)
{
return left;
}
}
if (cur->val < p->val && cur->val < q->val)
{
TreeNode* right = traversal(cur->right, p, q);
if (right != nullptr)
{
return right;
}
}
return cur;
}
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
return traversal(root, p, q);
}
};

边栏推荐
- 爬取国家法律法规数据库
- 一位平凡毕业生的大学四年
- binder hwbinder vndbinder
- Jinyuan's high-end IPO was terminated: it was planned to raise 750million Rushan assets and Liyang industrial investment were shareholders
- Market status and development prospect forecast of global epoxy resin active toughener industry in 2022
- openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
- Online text batch inversion by line tool
- 深度学习和神经网络的介绍
- Don't worry. This is the truth about wages in all industries in China
- Tupu digital twin intelligent energy integrated management and control platform
猜你喜欢

今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献

Google Earth Engine(GEE)——ImageCollection (Error)遍历影像集合产生的错误

过关斩将,擒“指针”(下)

华大单片机KEIL报错_WEAK的解决方案

《第五项修炼》(The Fifth Discipline):学习型组织的艺术与实践

xctf攻防世界 MISC薪手进阶区

Error reported by Huada MCU Keil_ Weak's solution

What is ICMP? What is the relationship between Ping and ICMP?

Bit.Store:熊市漫漫,稳定Staking产品或成主旋律

SQL Server - Window Function - 解决连续N条记录过滤问题
随机推荐
爬取国家法律法规数据库
Four years of College for an ordinary graduate
Market status and development prospect forecast of global active quality control air sampler industry in 2022
Erreur Keil de Huada Single Chip Computer La solution de Weak
Market status and development prospect forecast of the global shuttleless air jet loom industry in 2022
华大单片机KEIL报错_WEAK的解决方案
Bit. Store: long bear market, stable stacking products may become the main theme
Minmei new energy rushes to Shenzhen Stock Exchange: the annual accounts receivable exceeds 600million and the proposed fund-raising is 450million
破解仓储难题?WMS仓储管理系统解决方案
昱琛航空IPO被终止:曾拟募资5亿 郭峥为大股东
The IPO of Yuchen Airlines was terminated: Guozheng was proposed to raise 500million yuan as the major shareholder
Market status and development prospect forecast of global 3-Chloro-1,2-Propanediol industry in 2022
The Fifth Discipline: the art and practice of learning organization
GIS remote sensing R language learning see here
驾驭一切的垃圾收集器 -- G1
国际数字经济学院、华南理工 | Unified BERT for Few-shot Natural Language Understanding(用于小样本自然语言理解的统一BERT)
Tupu digital twin intelligent energy integrated management and control platform
Market status and development prospect forecast of phenethyl resorcinol for skin care industry in the world in 2022
Garbage collector driving everything -- G1
Photoshop layer related concepts layercomp layers move rotate duplicate layer compound layer