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

边栏推荐
- 【云驻共创】高校数字化差旅建设“解决之道”
- Is Guosen Securities a state-owned enterprise? Is it safe to open an account with Guosen Securities?
- Crawl national laws and Regulations Database
- OpenSSL client programming: SSL session failure caused by an obscure function
- 你知道 log4j2 各项配置的全部含义吗?带你了解 log4j2 的全部组件
- 破解仓储难题?WMS仓储管理系统解决方案
- 实战回忆录:从Webshell开始突破边界
- 基础数据类型和复杂数据类型
- Blink SQL built in functions
- 基于STM32F103ZET6库函数蜂鸣器实验
猜你喜欢

如何利用 RPA 实现自动化获客?

Blink SQL built in functions

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

Mathematical derivation from perceptron to feedforward neural network

Exporting coordinates of points in TXT format in ArcGIS

基于STM32F103ZET6库函数按键输入实验

Oracle 获取月初、月末时间,获取上一月月初、月末时间

binder hwbinder vndbinder

GIS remote sensing R language learning see here

从感知机到前馈神经网络的数学推导
随机推荐
Core dynamic Lianke rushes to the scientific innovation board: with an annual revenue of 170million yuan, Beifang Electronics Institute and Zhongcheng venture capital are shareholders
Oracle 获取月初、月末时间,获取上一月月初、月末时间
Solution of adding st-link to Huada MCU Keil
Online text batch inversion by line tool
Making single test so simple -- initial experience of Spock framework
Market status and development prospect forecast of phenethyl resorcinol for skin care industry in the world in 2022
昱琛航空IPO被终止:曾拟募资5亿 郭峥为大股东
Informatics Orsay all in one 1335: [example 2-4] connected block
可靠的分布式锁 RedLock 与 redisson 的实现
工作流自动化 低代码是关键
華大單片機KEIL報錯_WEAK的解决方案
Cdga | what is the core of digital transformation in the transportation industry?
工作流自动化 低代码是关键
Don't worry. This is the truth about wages in all industries in China
数仓的字符截取三胞胎:substrb、substr、substring
Market status and development prospect forecast of global 3-Chloro-1,2-Propanediol industry in 2022
信息学奥赛一本通 1333:【例2-2】Blah数集 | OpenJudge NOI 3.4 2729:Blah数集
Erreur Keil de Huada Single Chip Computer La solution de Weak
binder hwbinder vndbinder
maxwell 报错(连接为mysql 8.x)解决方法