当前位置:网站首页>Leetcode 106. construct binary tree from middle order and post order traversal sequence
Leetcode 106. construct binary tree from middle order and post order traversal sequence
2022-07-25 22:12:00 【LuZhouShiLi】
Leetcode 106. Construct binary tree from middle order and post order traversal sequence
subject
Given two arrays of integers inorder and postorder , among inorder Is the middle order traversal of a binary tree , postorder Is the post order traversal of the same tree , Please construct and return this Binary tree .
Ideas
- The last number of postorder traversal is the root node of the binary tree
- Find the corresponding position in the middle order traversal array according to the value of the root node , Then divide the middle order array into the left middle order traversal array , Right middle order traversal array
- Split the post order traversal array according to the length of the left and right middle order traversal array .
Code
/** * 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;
// The last element of the post order traversal array is the root node of the binary tree
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 delimiterIndex;
for(delimiterIndex = 0; delimiterIndex < inorder.size(); delimiterIndex++)
{
if(inorder[delimiterIndex] == rootValue)
{
break;
}
}
// Divide the medium order array
// Left closed right open interval :[0,delimiterIndex)
vector<int> leftInorder(inorder.begin(),inorder.begin() + delimiterIndex);
// [delimiterIndex + 1, end)
vector<int> rightInorder(inorder.begin() + delimiterIndex + 1,inorder.end());
// Discard end element
postorder.resize(postorder.size() - 1);
// Cut sequential array Divide according to the length of the middle order left array and the middle order right array
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
- Sofa weekly | open source person - Niu Xuewei, QA this week, contributor this week
- 6-17 vulnerability exploitation - deserialization remote command execution vulnerability
- The technical aspects of ByteDance are all over, but the result is still brushed. Ask HR why...
- Redis基础2(笔记)
- 2 lines of code to generate a solid desktop background
- 聚名十年,说出你的故事,百万豪礼等你拿
- 数据库进阶·如何针对所有用户数据中没有的数据去加入随机的数据-蜻蜓Q系统用户没有头像如何加入头像数据-优雅草科技kir
- Can I buy financial products with a revenue of more than 6% after opening an account
- Victoriametrics single node of kubernetes
猜你喜欢

Special class design

Application of breakthrough thinking in testing work

聚名十年,说出你的故事,百万豪礼等你拿

How to resolve a domain name to multiple IP addresses?

Don't know mock test yet? An article to familiarize you with mock
![[go basics 02] the first procedure](/img/af/f32762a828f384bf6aa063ebf959aa.png)
[go basics 02] the first procedure

H5幸运刮刮乐抽奖 免公众号+直运营

TS:typora代码片段缩进显示异常(已解决)-2022.7.24

Playwright tutorial (II) suitable for Xiaobai
![[Fantan] how to design a test platform?](/img/54/5aca54c0e66f8a7c1c3215b8f06613.png)
[Fantan] how to design a test platform?
随机推荐
What is partition and barrel division?
Preliminary study on Tesseract OCR
mysql: error while loading shared libraries: libncurses.so. 5: cannot open shared object file: No suc
SQL基本语句 DQL select与提取 DML插入删除
Don't vote, software testing posts are saturated
Uninstall NPM and install NPM_ Use 'NPM uninstall' to uninstall the NPM package 'recommended collection'
C语言:随机生成数+冒泡排序
6-18漏洞利用-后门连接
SQL basic statement DQL select and extract DML insert delete
[test development methodology] experience of test development platform PK - choice
Animation curves are used every day. Can you make one by yourself? After reading this article, you will!
数据库进阶·如何针对所有用户数据中没有的数据去加入随机的数据-蜻蜓Q系统用户没有头像如何加入头像数据-优雅草科技kir
Playwright tutorial (I) suitable for Xiaobai
Tfrecord write and read
What is class loading? Class loading process?
在进行自动化测试,遇到验证码的问题,怎么办?
Detailed summary of C language game dual cache to solve the flash screen problem [easy to understand]
JMeter websocket interface test
测试工作不受重视,你换位思考了吗?
QML module not found