当前位置:网站首页>Leetcode problem solving - 889 Construct binary tree according to preorder and postorder traversal
Leetcode problem solving - 889 Construct binary tree according to preorder and postorder traversal
2022-07-06 23:24:00 【100-1 = 0】
private int[] pre
private int[] post;
public TreeNode constructFromPrePost(int[] preorder, int[] postorder) {
pre = preorder;
post = postorder;
return buildTree(0,0,pre.length);
}
private TreeNode buildTree(int i,int j,int n){
if (n==0) return null;
TreeNode root = new TreeNode(pre[i++]);
if (n==1) return root;
int L = 1;
for (;L<n;L++) if (post[j+L-1]==pre[i]) break;
root.left = buildTree(i,j,L);
root.right = buildTree(i+L,j+L,n-1-L);
return root;
}
Ideas :
The first thing to know is
The former sequence traversal
[ The root node ,( The left subtree ),( Right subtree )]
After the sequence traversal
[( The left subtree ),( Right subtree ), The root node ]
that , We can know that the root node is the first element in the preorder traversal , So how to divide the left and right subtrees ? If we know the length of the left subtree, can we divide the left and right subtrees ? How to get the length ?
Where will the root node of the left subtree in the pre order traversal appear in the left subtree in the post order traversal ?
The answer is : the last one
That is to say :pre[1]==post[L-1]
set up i Traverse the first element index of the left subtree for the previous order ,j Traverse the index of the first element of the left subtree for the post order , Yes pre[i]=post[j+L-1]
Thus, the left and right subtrees can be divided for the next recursion
- take preorder and postorder Member variables stored in the class , It reduces the cumbersome process of transferring parameters
- At the beginning , The index of the first element of the pre order traversal of the left subtree is 1, But the answer is 0, This is because you need to create the root node first , After creating i It's going to increase 1, The index of the first element of the left subtree traversed by the latter order is 0, That is to say, it's passed on 0, And passed the length of the subtree node
- If the subtree length is 0, Go straight back to null, by 1, Then create the root node and return , Longer than 1, Continue to divide the left and right subtrees
- Loop to get the length of the left subtree , Continue to recursively create left and right subtrees
- return root
边栏推荐
- 机器人材料整理中的套-假-大-空话
- MySQL connected vscode successfully, but this error is reported
- MySQL数据库之JDBC编程
- (1) Chang'an chain learning notes - start Chang'an chain
- Can async i/o be implemented by UDF operator and then called by SQL API? At present, it seems that only datastre can be seen
- NFTScan 开发者平台推出 Pro API 商业化服务
- A few suggestions for making rust library more beautiful! Have you learned?
- CRMEB商城系统如何助力营销?
- Let me ask you if there are any documents or cases of flynk SQL generation jobs. I know that flynk cli can create tables and specify items
- Huawei cloud gaussdb (for redis) unveils issue 21: using Gauss redis to achieve secondary indexing
猜你喜欢

Station B boss used my world to create convolutional neural network, Lecun forwarding! Burst the liver for 6 months, playing more than one million
docker启动mysql及-eMYSQL_ROOT_PASSWORD=my-secret-pw问题解决

室内LED显示屏应该怎么选择?这5点注意事项必须考虑在内

Introduction to network basics

吴恩达2022机器学习课程评测来了!

What can be done for traffic safety?

JS addition, deletion, modification and query of JSON array
Detailed explanation of regular expression (regexp) in MySQL

儿童睡衣(澳大利亚)AS/NZS 1249:2014办理流程

浅谈现在的弊端与未来的发展
随机推荐
ACL 2022 | small sample ner of sequence annotation: dual tower Bert model integrating tag semantics
How does crmeb mall system help marketing?
The problem that dockermysql cannot be accessed by the host machine is solved
Interview question: AOF rewriting mechanism, redis interview must ask!!!
[step on pit collection] attempting to deserialize object on CUDA device+buff/cache occupy too much +pad_ sequence
AcWing 4299. Delete point
允许全表扫描 那个语句好像不生效set odps.sql.allow.fullscan=true;我
Spark Tuning (II): UDF reduces joins and judgments
The same job has two sources, and the same link has different database accounts. Why is the database list found in the second link the first account
每人每年最高500万经费!选人不选项目,专注基础科研,科学家主导腾讯出资的「新基石」启动申报...
Use mitmproxy to cache 360 degree panoramic web pages offline
None of the strongest kings in the monitoring industry!
自动更新Selenium驱动chromedriver
#DAYU200体验官# 在DAYU200运行基于ArkUI-eTS的智能晾晒系统页面
Ajout, suppression et modification d'un tableau json par JS
js导入excel&导出excel
Dayu200 experience officer homepage AITO video & Canvas drawing dashboard (ETS)
Children's pajamas (Australia) as/nzs 1249:2014 handling process
CRMEB商城系统如何助力营销?
Precise drag and drop within a contentable