当前位置:网站首页>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
边栏推荐
- ACL 2022 | small sample ner of sequence annotation: dual tower Bert model integrating tag semantics
- 今日睡眠质量记录78分
- flinksql select id ,count(*) from a group by id .
- Pdf batch splitting, merging, bookmark extraction, bookmark writing gadget
- PDF批量拆分、合并、书签提取、书签写入小工具
- 存币生息理财dapp系统开发案例演示
- Face recognition class attendance system based on paddlepaddle platform (easydl)
- How to choose the server system
- What does security capability mean? What are the protection capabilities of different levels of ISO?
- mysql查看表结构的三种方法总结
猜你喜欢
What can be done for traffic safety?
How to choose indoor LED display? These five considerations must be taken into account
Cloud native (32) | kubernetes introduction to platform storage system
NFTScan 开发者平台推出 Pro API 商业化服务
js对JSON数组的增删改查
浅谈网络安全之文件上传
With the help of this treasure artifact, I became the whole stack
Isomorphism + cross end, knowing applet +kbone+finclip is enough!
Dayu200 experience officer runs the intelligent drying system page based on arkui ETS on dayu200
使用MitmProxy离线缓存360度全景网页
随机推荐
Graphite document: four countermeasures to solve the problem of enterprise document information security
每日刷题记录 (十五)
今日睡眠质量记录78分
JDBC programming of MySQL database
这个『根据 op 值判断操作类型来自己组装 sql』是指在哪里实现?是指单纯用 Flink Tabl
基于PaddlePaddle平台(EasyDL)设计的人脸识别课堂考勤系统
前置机是什么意思?主要作用是什么?与堡垒机有什么区别?
Thinkphp5 multi table associative query method join queries two database tables, and the query results are spliced and returned
Should the jar package of MySQL CDC be placed in different places in the Flink running mode?
GPT-3当一作自己研究自己,已投稿,在线蹲一个同行评议
Two week selection of tdengine community issues | phase II
How to achieve text animation effect
NFTScan 开发者平台推出 Pro API 商业化服务
(flutter2) as import old project error: inheritfromwidgetofexacttype
Pytest unit test series [v1.0.0] [pytest execute unittest test case]
COSCon'22 社区召集令来啦!Open the World,邀请所有社区一起拥抱开源,打开新世界~
Dockermysql modifies the root account password and grants permissions
docker mysql5.7如何设置不区分大小写
JS addition, deletion, modification and query of JSON array
每人每年最高500万经费!选人不选项目,专注基础科研,科学家主导腾讯出资的「新基石」启动申报...