当前位置:网站首页>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
边栏推荐
- Dockermysql modifies the root account password and grants permissions
- Hard core observation 545 50 years ago, Apollo 15 made a feather landing experiment on the moon
- Pytest unit test series [v1.0.0] [pytest execute unittest test case]
- Stop saying that microservices can solve all problems
- Spark Tuning (II): UDF reduces joins and judgments
- DR-Net: dual-rotation network with feature map enhancement for medical image segmentation
- AI表现越差,获得奖金越高?纽约大学博士拿出百万重金,悬赏让大模型表现差劲的任务...
- 存币生息理财dapp系统开发案例演示
- 前置机是什么意思?主要作用是什么?与堡垒机有什么区别?
- A few suggestions for making rust library more beautiful! Have you learned?
猜你喜欢
[compilation principle] LR (0) analyzer half done
儿童睡衣(澳大利亚)AS/NZS 1249:2014办理流程
Implementation steps of mysql start log in docker
Koa2 addition, deletion, modification and query of JSON array
None of the strongest kings in the monitoring industry!
koa2对Json数组增删改查
(1) Chang'an chain learning notes - start Chang'an chain
MySQL connected vscode successfully, but this error is reported
Today's sleep quality record 78 points
Why are some people still poor and living at the bottom of society even though they have been working hard?
随机推荐
The problem of ASP reading Oracle Database
mysql-cdc 的jar包 ,在flink运行模式下,是不是要放在不同的地方呢?
[launched in the whole network] redis series 3: high availability of master-slave architecture
ICLR 2022 | 基于对抗自注意力机制的预训练语言模型
Spark Tuning (II): UDF reduces joins and judgments
DevSecOps软件研发安全实践——发布篇
Summary of three methods for MySQL to view table structure
企业不想换掉用了十年的老系统
How to choose the server system
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
Today's sleep quality record 78 points
Devsecops software R & D security practice - release
华为云GaussDB(for Redis)揭秘第21期:使用高斯Redis实现二级索引
Koa2 addition, deletion, modification and query of JSON array
Cocoscreator+typescripts write an object pool by themselves
Interview question: AOF rewriting mechanism, redis interview must ask!!!
前置机是什么意思?主要作用是什么?与堡垒机有什么区别?
不要再说微服务可以解决一切问题了
Method of canceling automatic watermarking of uploaded pictures by CSDN
Designed for decision tree, the National University of Singapore and Tsinghua University jointly proposed a fast and safe federal learning system