当前位置:网站首页>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);
}
};
边栏推荐
- Why is the integer type 128 to byte -128
- MySQL --- 子查询 - 列子查询(多行子查询)
- QML module not found
- 还不懂mock测试?一篇文章带你熟悉mock
- Minor GC 和 Full GC 有什么不同呢?
- Why does redisv6.0 introduce multithreading?
- Acwing 866. determining prime numbers by trial division
- Wechat card issuing applet source code - automatic card issuing applet source code - with flow main function
- YUV420 YUV420sp 图像格式「建议收藏」
- Can I buy financial products with a revenue of more than 6% after opening an account
猜你喜欢

Square root of X

After three years of software testing at Tencent, I was ruthlessly dismissed in July, trying to wake up my brother who was paddling

6-18 vulnerability exploitation - backdoor connection

win10搭建flutter环境踩坑日记

Basic knowledge in the project

磁盘空间的三种分配方式
![[dinner talk] those things that seem to be for the sake of the company but are actually incomprehensible (2: soft quality](/img/11/42c4674d23ee93850fb3d2de0d0932.png)
[dinner talk] those things that seem to be for the sake of the company but are actually incomprehensible (2: soft quality "eye edge" during interview)

How to implement an app application to limit users' time use?

After 2 years of functional testing, I feel like I can't do anything. Where should I go in 2022?

五种分配方式是否会产生内部碎片、外部碎片
随机推荐
[dinner talk] those things that seem to be for the sake of the company but are actually incomprehensible (2: soft quality "eye edge" during interview)
The technical aspects of ByteDance are all over, but the result is still brushed. Ask HR why...
还不懂mock测试?一篇文章带你熟悉mock
6-18漏洞利用-后门连接
启牛商学院和微淼商学院哪个靠谱?老师推荐的开户安全吗?
『Skywalking』.NET Core快速接入分布式链路追踪平台
Don't vote, software testing posts are saturated
Open source RSS subscriber freshrss
Tesseract OCR初探
【GO基础02】第一个程序
在进行自动化测试,遇到验证码的问题,怎么办?
What should I do if I encounter the problem of verification code during automatic testing?
EL表达式改进JSP
这次龙蜥展区玩的新花样,看看是谁的 DNA 动了?
Flex layout
Redis master-slave architecture lock failure problem (master-slave)
[51nod1676 undirected graph isomorphism] undirected graph hash [easy to understand]
面了个腾讯三年经验的测试员,让我见识到了真正的测试天花板
Wechat card issuing applet source code - automatic card issuing applet source code - with flow main function
在腾讯干软件测试3年,7月无情被辞,想给划水的兄弟提个醒...