当前位置:网站首页>Sword finger offer07 Rebuild binary tree
Sword finger offer07 Rebuild binary tree
2022-07-03 12:27:00 【Wu Liuqi】
The finger of the sword Offer07. Reconstruction of binary tree
Title Description
Enter the results of preorder traversal and inorder traversal of a binary tree , Build the binary tree and return its root node .
It is assumed that the results of the input preorder traversal and the middle order traversal do not contain repeated numbers .
Example 1:
Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]
Example 2:
Input: preorder = [-1], inorder = [-1]
Output: [-1]
Limit :
0 <= Number of nodes <= 5000
JAVA Code
Recursive method
Refer to the solution in tianqin book .
** design idea :** The first element in the sequence is traversed first x That is, the root node value of the tree . Found in middle order traversal x, utilize x Divide the array traversed in middle order into two subsequences , The left sequence forms the left subtree of the tree , The right sequence forms the right subtree of the tree , Then treat the left and right subsequences in the same way , Until there is only one element left in the processed subsequence , The construction of binary tree is completed .
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
int n = preorder.length;
TreeNode node = createTree(preorder,inorder,0,n-1,0,n-1);
return node;
}
// Building a binary tree
public TreeNode createTree(int[] preorder, int[] inorder,int L1,int R1,int L2,int R2){
TreeNode node;
int i;
if(L1>R1){
return null;
}
node = new TreeNode();
node.left=node.right=null;
for(i = L2;i<=R2;++i){
if(inorder[i]==preorder[L1]){
break;
}
}
node.val = inorder[i];
node.left = createTree(preorder,inorder,L1+1,L1+i-L2,L2,i-1);
node.right = createTree(preorder,inorder,L1+i-L2+1,R1,i+1,R2);
return node;
}
}

official – Iterative method
Iterative non recursive method .
The idea can be based on the non recursive implementation of binary tree traversal .
The non recursive method of binary tree is hard to understand , So I'll add it here when I have learning needs in the future .
For the time being, I feel that recursion is enough . I can't understand it, so I don't bother to waste time .
边栏推荐
- SLF4J 日志门面
- Oracle advanced (I) realize DMP by expdp impdp command
- QT OpenGL texture map
- Flutter 退出登录二次确认怎么做才更优雅?
- Laravel time zone timezone
- Unicode查询的官方网站
- Itext7 uses iexternalsignature container for signature and signature verification
- (database authorization - redis) summary of unauthorized access vulnerabilities in redis
- The difference between lambda and anonymous inner class
- 1-2 project technology selection and structure
猜你喜欢

Colleagues wrote a responsibility chain model, with countless bugs

Integer int compare size

Symlink(): solution to protocol error in PHP artisan storage:link on win10

PHP export word method (phpword)

If you can't learn, you have to learn. Jetpack compose writes an im app (I)

Summary of development issues

Qt+vtk+occt reading iges/step model

Prompt unread messages and quantity before opening chat group

LeetCode 0556.下一个更大元素 III - 4步讲完

4000 word super detailed pointer
随机推荐
Lambda expression
error: expected reference but got (raw string)
LeetCode 0556. Next bigger element III - end of step 4
(construction notes) ADT and OOP
Fluent: Engine Architecture
111. Minimum depth of binary tree
JVM memory model
The difference between lambda and anonymous inner class
Use of atomicinteger
2.9 overview of databinding knowledge points
If you can't learn, you have to learn. Jetpack compose writes an im app (I)
Cloud Computing future - native Cloud
DEJA_VU3D - Cesium功能集 之 053-地下模式效果
Talk about the state management mechanism in Flink framework
239. Sliding window maximum
在网上炒股开户可以吗?资金安全吗?
网络通讯之Socket-Tcp(一)
20. Valid brackets
Introduction to concurrent programming (I)
Colleagues wrote a responsibility chain model, with countless bugs