当前位置:网站首页>429- binary tree (108. convert the ordered array into a binary search tree, 538. convert the binary search tree into an accumulation tree, 106. construct a binary tree from the middle order and post o
429- binary tree (108. convert the ordered array into a binary search tree, 538. convert the binary search tree into an accumulation tree, 106. construct a binary tree from the middle order and post o
2022-06-27 19:51:00 【liufeng2023】
108. Convert an ordered array to a binary search tree
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. Convert binary search tree to accumulation tree
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. Construct binary tree from middle order and post order traversal sequence
class Solution {
public:
TreeNode* traversal(vector<int>& inorder, vector<int>& postorder) {
if (postorder.size() == 0) return nullptr;
// After traversing the last element of the array , As the current intermediate node
int rootValue = postorder[postorder.size() - 1];
TreeNode* root = new TreeNode(rootValue);
// Leaf node
if (postorder.size() == 1) return root;
// Find the cutting point of the middle order traversal
int del;
for (del = 0; del < inorder.size(); del++)
{
if (inorder[del] == rootValue) break;
}
// Cut the ordered array
// Left closed right open interval :[0, delimiterIndex)
vector<int> leftInorder(inorder.begin(), inorder.begin() + del);
vector<int> rightInorder(inorder.begin() + del + 1, inorder.end());
// postorder Discard end element
postorder.resize(postorder.size() - 1);
// Cut subsequent arrays
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. The nearest common ancestor of a binary search tree
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);
}
};
边栏推荐
- 【建议收藏】ABAP随笔-EXCEL-4-批量导入-推荐
- Leetcode 821. 字符的最短距离(简单) - 续集
- 券商经理的开户二维码开户买股票安全吗?有谁知道啊
- Photoshop-图层相关概念-LayerComp-Layers-移动旋转复制图层-复合图层
- Leetcode 1381. 设计一个支持增量操作的栈
- UE4-Actor基础知识
- Function key input experiment based on stm32f103zet6 Library
- 作用域-Number和String的常用Api(方法)
- Substrate及波卡一周技术更新速递 20220425 - 20220501
- GIS遥感R语言学习看这里
猜你喜欢
GIS遥感R语言学习看这里
Embracing cloud Nativity: Practice of Jiangsu Mobile order center
Oracle 获取月初、月末时间,获取上一月月初、月末时间
Array exercises follow up
International School of Digital Economics, South China Institute of technology 𞓜 unified Bert for few shot natural language understanding
海底电缆探测技术总结
实战回忆录:从Webshell开始突破边界
今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
基于STM32F103ZET6库函数按键输入实验
SQL Server - Window Function - 解决连续N条记录过滤问题
随机推荐
429-二叉树(108. 将有序数组转换为二叉搜索树、538. 把二叉搜索树转换为累加树、 106.从中序与后序遍历序列构造二叉树、235. 二叉搜索树的最近公共祖先)
你知道 log4j2 各项配置的全部含义吗?带你了解 log4j2 的全部组件
这个是和数据采集一样,可以定义一个参数为上个月或者前一天,然后在sql中使用这个参数吗?
shell脚本常用命令(四)
mime.type文件内容
Adding, deleting, modifying and querying MySQL tables (basic)
高收益银行理财产品在哪里看?
华大单片机KEIL添加ST-LINK解决方法
Solution of adding st-link to Huada MCU Keil
GIS remote sensing R language learning see here
Kotlin微信支付回调后界面卡死并抛出UIPageFragmentActivity WindowLeaked
rust 中的结构体
一对一关系
Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
Photoshop layer related concepts layercomp layers move rotate duplicate layer compound layer
MySQL初学者福利
crontab的学习随笔
基于STM32F103ZET6库函数外部中断实验
Redis 原理 - String
Embracing cloud Nativity: Practice of Jiangsu Mobile order center