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

边栏推荐
猜你喜欢

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

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

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

Online text batch inversion by line tool

binder hwbinder vndbinder

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

嵌入式软件开发中必备软件工具

海底电缆探测技术总结

Error reported by Huada MCU Keil_ Weak's solution

The Fifth Discipline: the art and practice of learning organization
随机推荐
Golang map 并发读写问题源码分析
openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
C# 二维码生成、识别,去除白边、任意颜色
1024 Palindromic Number
Is the account opening QR code given by CICC securities manager safe? Who can I open an account with?
ABAP随笔-通过api获取新冠数据
网络上开户买股票是否安全呢?刚接触股票,不懂求指导
如何利用 RPA 实现自动化获客?
Leetcode 989. 数组形式的整数加法(简单)
判断一个变量是数组还是对象?
MASS幸运哈希游戏系统开发丨冲突解决方法(代码分析)
Oracle 获取月初、月末时间,获取上一月月初、月末时间
Solution of adding st-link to Huada MCU Keil
Workflow automation low code is the key
SQL Server - Window Function - 解决连续N条记录过滤问题
ABAP随笔-面试回忆 望大家 需求不增 人天飙升
Memoirs of actual combat: breaking the border from webshell
PyCharm常用功能 - 断点调试
ABAP-CL_OBJECT_COLLECTION工具类
GIS遥感R语言学习看这里