当前位置:网站首页>Leetcode 106. 从中序与后序遍历序列构造二叉树
Leetcode 106. 从中序与后序遍历序列构造二叉树
2022-07-25 22:11:00 【LuZhouShiLi】
Leetcode 106. 从中序与后序遍历序列构造二叉树
题目
给定两个整数数组 inorder 和 postorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。
思路
- 后序遍历的最后一个数就是二叉树的根节点
- 根据根节点的值在中序遍历数组中找到对应的位置,然后分割中序数组为左中序遍历数组,右中序遍历数组
- 根据左右中序遍历数组的长度来拆分后序遍历数组。
代码
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
TreeNode* traversal(vector<int>& inorder,vector<int>& postorder)
{
if(postorder.size() == 0) return NULL;
// 后序遍历数组的最后一个元素就是二叉树的根节点
int rootValue = postorder[postorder.size() - 1];
TreeNode* root = new TreeNode(rootValue);
// 叶子节点
if(postorder.size() == 1)
{
return root;
}
// 找到中序遍历的切割点
int delimiterIndex;
for(delimiterIndex = 0; delimiterIndex < inorder.size(); delimiterIndex++)
{
if(inorder[delimiterIndex] == rootValue)
{
break;
}
}
// 划分中序数组
// 左闭右开区间:[0,delimiterIndex)
vector<int> leftInorder(inorder.begin(),inorder.begin() + delimiterIndex);
// [delimiterIndex + 1, end)
vector<int> rightInorder(inorder.begin() + delimiterIndex + 1,inorder.end());
// 舍弃末尾元素
postorder.resize(postorder.size() - 1);
// 切割后序数组 根据中序左数组和中序右数组的长度进行划分
vector<int> leftPosterorder(postorder.begin(),postorder.begin() + leftInorder.size());
vector<int> rightPosterorder(postorder.begin() + leftInorder.size(),postorder.end());
root->left = traversal(leftInorder,leftPosterorder);
root->right = traversal(rightInorder,rightPosterorder);
return root;
}
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
if(inorder.size() == 0 || postorder.size() == 0)
{
return NULL;
}
return traversal(inorder,postorder);
}
};
边栏推荐
- Internship: writing common tool classes
- Application of breakthrough thinking in testing work
- 3. Editors (vim)
- YUV420 yuv420sp image format "recommended collection"
- 『SignalR』.NET使用 SignalR 进行实时通信初体验
- Detailed summary of C language game dual cache to solve the flash screen problem [easy to understand]
- Ansible+cronab batch deployment patrol
- C语言:随机生成数+冒泡排序
- Special class design
- Guiding principles of information security construction
猜你喜欢

这次龙蜥展区玩的新花样,看看是谁的 DNA 动了?

面了个腾讯三年经验的测试员,让我见识到了真正的测试天花板

『Skywalking』.NET Core快速接入分布式链路追踪平台

Jmeter---设置代理录制请求

五种分配方式是否会产生内部碎片、外部碎片

Solutions to the failure of win key in ikbc keyboard

2年功能测试,却感觉自己什么都不会,2022我该何去何从?

Children's programming electronic society graphical programming level examination scratch level 1 real problem analysis (judgment question) June 2022

Imitation Tiktok homepage interface

6-18漏洞利用-后门连接
随机推荐
『Skywalking』. Net core fast access distributed link tracking platform
TS:typora代码片段缩进显示异常(已解决)-2022.7.24
win10搭建flutter环境踩坑日记
Redis为何选择单线程?
什么是分区分桶?
虚拟内存与磁盘
The reisson distributed lock renewal failed due to network reasons, resulting in the automatic release of the lock when the business is still executing but the lock is not renewed.
How to quickly build a picture server [easy to understand]
Solutions to the failure of win key in ikbc keyboard
聚名十年,说出你的故事,百万豪礼等你拿
kubernetes之VictoriaMetrics单节点
Redis内存淘汰机制?
JMeter websocket interface test
The file cannot be saved (what if the folder is damaged and cannot be read)
自动化测试岗花20K招人,到最后居然没一个合适的,招两个应届生都比他们强吧
internship:普通常用的工具类编写
关于接口测试你想知道的都在这儿了
[51nod1676 undirected graph isomorphism] undirected graph hash [easy to understand]
What is the difference between minor GC and full GC?
How to implement an app application to limit users' time use?