当前位置:网站首页>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
边栏推荐
- Let's see through the network i/o model from beginning to end
- 人均瑞数系列,瑞数 4 代 JS 逆向分析
- 让我们,从头到尾,通透网络I/O模型
- Thinkphp5 multi table associative query method join queries two database tables, and the query results are spliced and returned
- CRMEB商城系统如何助力营销?
- Bipartite graph determination
- 企業不想換掉用了十年的老系統
- koa2对Json数组增删改查
- [compilation principle] LR (0) analyzer half done
- 室内LED显示屏应该怎么选择?这5点注意事项必须考虑在内
猜你喜欢
云原生(三十二) | Kubernetes篇之平台存储系统介绍
Gpt-3 is a peer review online when it has been submitted for its own research
UE4 blueprint learning chapter (IV) -- process control forloop and whileloop
Use mitmproxy to cache 360 degree panoramic web pages offline
为了交通安全,可以做些什么?
dockermysql修改root账号密码并赋予权限
Case recommendation: An Qing works with partners to ensure that the "smart court" is more efficient
koa2对Json数组增删改查
Example code of MySQL split string as query condition
How to choose indoor LED display? These five considerations must be taken into account
随机推荐
Interview question: AOF rewriting mechanism, redis interview must ask!!!
Face recognition class attendance system based on paddlepaddle platform (easydl)
Isomorphism + cross end, knowing applet +kbone+finclip is enough!
Huawei cloud gaussdb (for redis) unveils issue 21: using Gauss redis to achieve secondary indexing
flinksql select id ,count(*) from a group by id .
Nftscan Developer Platform launches Pro API commercial services
CRMEB商城系统如何助力营销?
European Bioinformatics Institute 2021 highlights report released: nearly 1million proteins have been predicted by alphafold
mysql-cdc 的jar包 ,在flink运行模式下,是不是要放在不同的地方呢?
新手问个问题,我现在是单机部署的,提交了一个sql job运行正常,如果我重启了服务job就没了又得
AcWing 4299. Delete point
asp读取oracle数据库问题
Station B boss used my world to create convolutional neural network, Lecun forwarding! Burst the liver for 6 months, playing more than one million
CRMEB 商城系统如何助力营销?
TDengine 社区问题双周精选 | 第二期
Cloud native technology container knowledge points
前置机是什么意思?主要作用是什么?与堡垒机有什么区别?
面试题:AOF重写机制,redis面试必问!!!
Flutter life cycle
The worse the AI performance, the higher the bonus? Doctor of New York University offered a reward for the task of making the big model perform poorly